Renganathan V Renganathan V - 1 year ago 105
AngularJS Question

Instantiating angular js controller


angular.module("myApp" , [subApp1]); //line1


import mycontroller from './mycontroller';
$stateProvider.state('home', {
templateUrl: './app/home.html',
controller : myController //line 2
.controller('myController',['$scope','$http', myController]]); //line 3

Issue :

Following is my home.html.
Grid is directive that is working fine if i source data from json using ajax.but not working if i get scope data (same json) from controller.

<grid data-type="scope" data="mydata"></dashgrid>

In controller am getting data using $http.get that is working fine.but still its not getting populated in grid.

Error: Uncaught (in promise) TypeError: Cannot read property 'columns' of undefined(…)

1)Is http get asynchronous ? whether grid getting loaded before actually data is loaded by $http.get
1) In this case when the controller will get instantiated (line1 or line 2 or line3 )?
2) what is difference between line 2 and line 3 ? Is line 3 necessary ?

Answer Source

Yes, http is asynchronous.

Yes, the view is displayed without waiting for your get request to complete and you must thus make sure it works fine in that situation (by not displaying the grid while its data is not there, for example, using ng-if). The alternative is to use resolve in the state configuration.

The controller is instantiated when the router switches to the home state.

Line 3 registers a controller to the module under the name 'myController'. Line 2 tells the router that the view of the home state is controlled by myController

Line 3 is necessary if you intend to use ng-controller="myController" in some view, or if you want to configure a route by specifying the name of the controller rather than the controller function itself:

controller : 'myController'

instead of

controller : myController
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download