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
Post a Comment