Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Can we use angular js ng init directive for Controller initialize? #13530

@Jeevanandanj

Description

@Jeevanandanj

After developing an application, I had come across with great confusion.

can we use ngInit directive for Controller to initialize or get the data for the current view (controller)?

I am using ngInit for controller, since sometime the scripts are loading two times. If I call a server side method, in the controller function end, then two calls will go to the server.

If ngInit for controller is wrong to initialize data, can you please suggest a solution.

EXAMPLE

app.controller('testcontrol', ['$scope', '$http', function($scope, $http) {
    $scope.resultset = [];
    $scope.GetData =function(){
       $http.get('http://localhost:3000/testdata').then(function(data) {
          $scope.resultset = data.data.resultset;
          console.log($scope.resultset);
      },function(error) {
        $scope.error = error;
      })
    }

    $scope.init=function(){
        $scope.GetData();
    }
}]);

HTML:

<div class="wrapper-md" ng-init="init()" ng-controller="testcontrol">
    <ul>
        <li data-ng-repeat="result in resultset">
            {{result.name}}
        </li>
    </ul>
</div>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions