$watch not detecting changes in service variable

元气小坏坏 提交于 2020-01-17 03:35:48

问题


I am new to angularjs and I am facing this strange problem where $watch is not firing on change of a variable in service. I don't know where I have made a mistake.

Plunkr: Link to Plunkr

script.js file:

var app = angular.module("app", []);
app.factory("loggedInUser", [loggedInUser]);

function loggedInUser() {
  var currentLoggedInUser = undefined;
  setTimeout(function() {
    currentLoggedInUser = "Karthik";
    console.log("Updated user name");
  }, 3000);
  return {
    getLoggedInUser: function() {
      return currentLoggedInUser;
    }
  };
}

app.controller("ctrl", ["$scope", "loggedInUser", ctrl]);

function ctrl($scope, loggedInUser) {
  var vm = this;
  $scope.$watch(function() {
    return loggedInUser.getLoggedInUser();
  }, function() {
    vm.loggedInUserName = loggedInUser.getLoggedInUser();
    console.log("Inside Watch");
  },true);
} 

HTML:

<html>

  <head>
    <script src="https://opensource.keycdn.com/angularjs/1.5.8/angular.min.js" data-require="angularjs@1.5.8" data-semver="1.5.8"></script>
    <link href="style.css" rel="stylesheet" />
    <script src="script.js"></script>
  </head>

  <body ng-app="app">
    <h1 ng-controller="ctrl as vm">
      Hello <span ng-bind="vm.loggedInUserName"></span>
    </h1>
  </body>

</html>

回答1:


use $timeout instead of setTimeout ? .. cause it need$scope.$apply (outside angular event) .. $timeout is a angular wrapping for setTimeout .. something like:

 // Code goes here

var app = angular.module("app", []);
app.factory("loggedInUser", ["$timeout", loggedInUser]);

function loggedInUser($timeout) {
  var currentLoggedInUser;
   $timeout(function() {
    currentLoggedInUser = "Karthik";
    console.log("Updated user name");
  }, 3000);
  return {
    getLoggedInUser: function() {

      return currentLoggedInUser;
    }
  };
}

app.controller("ctrl", ["$scope", "loggedInUser", ctrl]);

function ctrl($scope, loggedInUser) {
  var vm = this;
  $scope.$watch(function() {
    return loggedInUser.getLoggedInUser();
  }, function() {
    vm.loggedInUserName = loggedInUser.getLoggedInUser();
    console.log("Inside Watch");
  },true);
}

BUT .. IF YOU WANT TO USE instead SetTimeout function:

  // Code goes here

var app = angular.module("app", []);
app.factory("loggedInUser", ["$rootScope",loggedInUser]);

function loggedInUser($rootScope) {
  var currentLoggedInUser = undefined;
  setTimeout(function() {
    $rootScope.$apply(function(){
    currentLoggedInUser = "Karthik";
    console.log("Updated user name");
    });
  }, 3000);
  return {
    getLoggedInUser: function() {
      return currentLoggedInUser;
    }
  };
}

app.controller("ctrl", ["$scope", "loggedInUser", ctrl]);

function ctrl($scope, loggedInUser) {
  var vm = this;
  $scope.$watch(function() {
    return loggedInUser.getLoggedInUser();
  }, function() {
    vm.loggedInUserName = loggedInUser.getLoggedInUser();
    console.log("Inside Watch");
  },true);
}


来源:https://stackoverflow.com/questions/41720133/watch-not-detecting-changes-in-service-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!