gpt4 book ai didi

javascript - 仅针对一个 Controller 未定义 Angular 服务字段

转载 作者:行者123 更新时间:2023-11-28 19:09:42 25 4
gpt4 key购买 nike

我定义了一个简单的服务:

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

我用它来设置和共享 Controller 之间的身份验证状态。在我的 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);
});
}
};

//...

}]);

与 MainCtrl 中的操作一样:

    app.controller('MainCtrl', ['$scope', '$location', '$window', 'UserService', 'AuthenticationService', 
function MainCtrl($scope, $location, $window, UserService, AuthenticationService) {

//...

$scope.logout = function() {
console.log("LOGOUT CALLED AT MAIN CONTROLLER. isLogged "
+ AuthenticationService.isLogged); //prints true
if (AuthenticationService.isLogged) {
AuthenticationService.isLogged = false;
delete $window.sessionStorage.token;
$location.path("/");
}
};

}]);

但是无法从此 Controller 访问它,即使我很确定我正确注入(inject)了服务:

    app.controller('SearchCtrl', ['$scope', '$location', '$window', 'MovieDataService', 'UserService', 'AuthenticationService', 
function SearchCtrl($scope, $location, $window, UserService, AuthenticationService, MovieDataService) {

//...

$scope.logout = function() {
console.log("LOGOUT CALLED AT SEARCH CONTROLLER. isLogged: "
+ AuthenticationService.isLogged); //prints undefined
if (AuthenticationService.isLogged) {
//UserService.logout();
AuthenticationService.isLogged = false;
delete $window.sessionStorage.token;
$location.path("/");
}
};
//...
}]);

为什么会发生这种情况?我不会在任何地方取消 isLogged 设置。

最佳答案

app.controller('SearchCtrl', ['$scope', '$location', '$window', 'MovieDataService', 'UserService', 'AuthenticationService', 
function SearchCtrl($scope, $location, $window, UserService, AuthenticationService, MovieDataService) {

应该是

app.controller('SearchCtrl', ['$scope', '$location', '$window', 'MovieDataService', 'UserService', 'AuthenticationService', 
function SearchCtrl($scope, $location, $window, MovieDataService, UserService, AuthenticationService) {

换句话说,您需要确保函数中参数的顺序与前面的参数名称/依赖项列表相匹配。

引用AngularJS DI documentation (强调我的):

Inline Array Annotation

This is the preferred way to annotate application components. This is how the examples in the documentation are written.

For example:

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

Here we pass an array whose elements consist of a list of strings (the names of the dependencies) followed by the function itself.

When using this type of annotation, take care to keep the annotation array in sync with the parameters in the function declaration.

关于javascript - 仅针对一个 Controller 未定义 Angular 服务字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30948732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com