JavaScript和AngularJS仍然是新的我。
JavaScript and AngularJS is still new to me.
我从我的服务麻烦变量进入我的范围。下面是我在做什么:
I have trouble getting variables from my services into my scope. Here is what I'm doing:
myControllers.controller('UserController', function($scope, UserService) {
clearLoginForm();
updateStuff();
$scope.notifications = UserService.notifications;
function updateStuff() {
$scope.realName = UserService.realName;
$scope.loggedIn = UserService.loggedIn;
}
function clearLoginForm() {
$scope.loginName = '';
$scope.loginPassword = '';
}
$scope.login = function() {
UserService.login($scope.loginName,$scope.loginPassword);
updateStuff();
clearLoginForm();
}
$scope.logout = function() {
UserService.logout();
updateStuff();
clearLoginForm();
}
});
将UserService都应保存在用户登录和登录/注销功能和帐户相关的东西,应该从服务器轮询的信息。
the UserService should hold the information about the logged in User and the functions for login/logout and account related stuff that should be polled from the server.
myModule.factory('UserService', function($interval) {
var loggedIn = false;
var realName = "";
var notifications = {};
resetNotifications();
function resetNotifications() {
notifications.msgCount = 0;
notifications.todoCount = 0;
notifications.ergCount = 0;
}
function login(name, password) {
if (password === 'abc') {
loggedIn = true;
realName = 'John Smith';
}
};
function logout() {
loggedIn = false;
realName = '';
resetNotifications();
}
function updateNotifications() {
if (loggedIn) {
notifications.msgCount = 1;
}
else {
resetNotifications();
}
};
$interval(updateNotifications, 10000);
return {
loggedIn : loggedIn,
realName : realName,
login : login,
logout : logout,
notifications : notifications
};
});
但它不工作。所以,我注意到,如果我在登录/注销功能改变的loggedIn到this.loggedIn(和真实姓名一致)则获得价值传播到的范围。为什么我需要的本?不是的loggedIn和真实姓名在我的封闭?但是,这不是因为这样的解决方案,现在比updateNotifications功能使用不同的loggedIn(在这里我不能改变它为this.loggedIn)。
But it's not working. So I noticed that if I change "loggedIn" in the login/logout functions to "this.loggedIn" (and same with realName) then the values get propagated to the scope. Why do I need the "this"? Aren't "loggedIn" and "realName" in my closure? But this is not a solution since this is now a different "loggedIn" than used in the updateNotifications function (and here I can't change it to "this.loggedIn").
其次,我不喜欢,我需要每次调用updateStuff()函数在服务变化的值。但它不工作没有。有没有更好的办法做到这一点?
Secondly, I don't like that I need to call the updateStuff() Function everytime a value in the service changes. But it's not working without. Is there a better way to do it?
我怕我在做某种根本性错误。
I fear I'm doing something fundamentally wrong.
推荐答案
当您返回的loggedIn
为您服务结束时,你实际上是返回一个副本变量。所以,当你以后UserService内更新,副本将不会被更新。解决最简单的方法是用吸气方式:
When you return loggedIn
at the end of your service, what you're really returning is a copy of the variable. So when you later update it inside of UserService, the copy won't get updated. The easiest way to solve that is with the getter approach:
function getLoggedIn() { return loggedIn; }
return {
getLoggedIn: getLoggedIn
};
相关推荐
最新文章