Reputation: 523
myApp.js
angular.module("myApp" , [subApp1]); //line1
subapp1.js
import mycontroller from './mycontroller';
angular.module("subApp1",[])
.config(['$stateProvider','$urlRouteProvider',function($stateProvider,$urlRouteProvider){
$stateProvider.state('home', {
url:'/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(…)
Questions: 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 ?
Upvotes: 0
Views: 32
Reputation: 692121
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
Upvotes: 1