【发布时间】:2014-01-18 12:46:19
【问题描述】:
我正在编写一个角度指令,它有一个名为 init() 的函数,该函数需要在页面加载时运行,并且任何时候两个变量发生变化。我从这个开始:
link: function(scope, elem, attrs){
init();
//Refresh the view when the the type or typeId changes
scope.$watch("[type,typeId]", function(){init(scope)}, true);
},
但我注意到 init() 实际上被调用了两次。在我取出第一个 init() 之后,它显然只会运行一次,但我只是好奇,这是预期的行为吗?
我认为 watch 函数只有在发生变化时才会执行。是因为它们在技术上正在“改变”(在链接后被初始化?)还是只是 $watch 立即执行函数的自然行为?
【问题讨论】:
标签: javascript angularjs angularjs-directive watch