javascript - Angular service field is undefined for one controller only -


i have defined simple service:

app.service('authenticationservice', function() {   var auth = {     islogged: false   };   return auth; }); 

i use set , share authentication state between controllers. reads fine in loginctrl:

app.controller('loginctrl', ['$scope', '$location', '$window', 'userservice', 'authenticationservice',    function loginctrl($scope, $location, $window, userservice, authenticationservice) {      $scope.login = function login(username, password) {       if (username !== undefined && password !== undefined) {         userservice.login(username, password)         .success(function(data) {           authenticationservice.islogged = true; //sets value correctly           $window.sessionstorage.token = data.token;           $location.path("/main");         })         .error(function(status, data) {           console.log(status);           console.log(data);         });       }     };      //...  }]); 

as in mainctrl:

    app.controller('mainctrl', ['$scope', '$location', '$window', 'userservice', 'authenticationservice',        function mainctrl($scope, $location, $window, userservice, authenticationservice) {        //...        $scope.logout = function() {         console.log("logout called @ main controller. islogged "  + authenticationservice.islogged); //prints true         if (authenticationservice.islogged) {           authenticationservice.islogged = false;           delete $window.sessionstorage.token;           $location.path("/");         }       };      }]); 

however inaccessible controller, though i'm pretty sure i'm injecting service correctly:

    app.controller('searchctrl', ['$scope', '$location', '$window', 'moviedataservice', 'userservice', 'authenticationservice',        function searchctrl($scope, $location, $window, userservice, authenticationservice, moviedataservice) {      //...        $scope.logout = function() {         console.log("logout called @ search controller. islogged: "  + authenticationservice.islogged); //prints undefined         if (authenticationservice.islogged) {           //userservice.logout();           authenticationservice.islogged = false;           delete $window.sessionstorage.token;           $location.path("/");         }       };     //...     }]); 

why happening? i'm not unsetting islogged anywhere.

app.controller('searchctrl', ['$scope', '$location', '$window', 'moviedataservice', 'userservice', 'authenticationservice',    function searchctrl($scope, $location, $window, userservice, authenticationservice, moviedataservice) { 

should be

app.controller('searchctrl', ['$scope', '$location', '$window', 'moviedataservice', 'userservice', 'authenticationservice',    function searchctrl($scope, $location, $window, moviedataservice, userservice, authenticationservice) { 

in other words, need make sure order of arguments in function matches preceding list of argument names/dependencies.

to quote angularjs di documentation (emphasis mine):

inline array annotation

this preferred way annotate application components. how examples in documentation written.

for example:

somemodule.controller('mycontroller', ['$scope', 'greeter', function($scope, greeter) {   // ... }]); 

here pass array elements consist of list of strings (the names of dependencies) followed function itself.

when using type of annotation, take care keep annotation array in sync parameters in function declaration.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -