javascript - 从 Angular watch 中调用函数

标签 javascript angularjs

如何从 $scope.watch 函数内部调用 Controller 中的函数? 我尝试过类似的方法,但这不起作用

$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();
  } 
});

关于javascript - 从 Angular watch 中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40798989/

相关文章:

javascript - 如何使用 XSL 从 XML 创建可折叠的可扩展菜单树

javascript - 是否可以找到与特定 HTML 片段相关联的 JavaScript 事件?

jquery - 点击标签触发两次滑动动画

javascript - html 页面中的突出显示文本不匹配

JavaScript 继承问题

javascript - 滚动到全彩时的透明标题 - 导航覆盖问题

javascript - AngularJS:如何优化嵌套指令

javascript - 如何处理项目点击从 ng-repeat 生成的列表并加载下一页

javascript - 具有数组长度条件的 Angular ng-repeat

javascript - Angular js post/get 嵌套 ng-repeat