在vanilla JS中,我可以监听特定DOM元素的事件:el.addEventListener('click',function(){});
。是否有一种与angularjs上的$ on方法相同的方法,并听取特定的范围?
答案 0 :(得分:1)
如果你想听一个特定的范围值,我建议你使用Angular的$ method service。
以下是如何使用它的示例:
scope.$watch('name', function(newValue, oldValue) {
scope.counter = scope.counter + 1;
});
也:
scope.$watch(
// This function returns the value being watched. It is called for each turn of the $digest loop
function() { return food; },
// This is the change listener, called when the value returned from the above function changes
function(newValue, oldValue) {
if ( newValue !== oldValue ) {
// Only increment the counter if the value changed
scope.foodCounter = scope.foodCounter + 1;
}
}
);