javascript – 从角度观察中调用函数

如何从$scope.watch函数中调用控制器中的函数?

我尝试这样的东西,但这不起作用

$scope.$watch(function(){return self.user}, function (newVal, oldVal, scope){
if (self.user) {
    getNotificationCount();
  } 
});

var getNotificationCount = function(){
  console.log("called your function");
}

它给了我一个错误

TypeError: getNotificationCount is not a function

最佳答案 您需要在调用之前定义getNotificationCount.

var getNotificationCount = function(){
  console.log("called your function");
}

$scope.$watch(function(){return self.user}, function (newVal, oldVal, scope){
  if (self.user) {
    getNotificationCount();
  } 
});
点赞