angularjs:在服务变量没有更新服务或控制器 [英] angularjs: variables in service not updating for service or controller

查看:116
本文介绍了angularjs:在服务变量没有更新服务或控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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
};

这篇关于angularjs:在服务变量没有更新服务或控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆