angularjs - Cannot access $rootScope property from controller? -
in angular's run block setting 1 property on $rootscope when controller executes property doesn't exists on $rootscope.
when application starts, in debugger see "adal:loginsuccess" handler gets executed , $rootscope.isadmin sets "true", when controller start executing $rootscope.isadmin doesnt exists on $rootscope. why? below code
app.js
var main = angular.module('mymodule', [ 'nganimate','ngroute','mycontrollers','adalangular'... ]); main.config(['$httpprovider','adalauthenticationserviceprovider', function ($httpprovider,adalprovider) { adalprovider.init( { // config azure client id here }, $httpprovider ); }]); main.run(["$rootscope", "$state", "api", "usspinnerservice", function ($rootscope, $state, api, usspinnerservice,) { $rootscope.$on("adal:loginsuccess", function () { $rootscope.isadmin = user.isadmin; }); }]);
controller.js
angular.module('mycontrollers', []) .controller('headerctrl', ['$rootscope','$scope', 'adalauthenticationservice', function ($rootscope, $scope, adalauthenticationservice) { //$rootscope.isadmin set in angular's run block above //however $rootscope.isadmin not exists here, why????? }])
because have var main = angular.module('mymodule', [...])
, app named 'mymodule'; however, controllers have angular.module('mycontrollers', [])
. you've done instantiate new instance of angular. angular bootstrap first instance of instantiation, need use angular.bootstrap
, i'm guessing that's not want. instead, replace angular.module('mycontrollers', [])
angular.module('mymodule').controller(...)
. notice lack of brackets. you're using module instantiated.
Comments
Post a Comment