在项目发生变化时不会调用 Watch。它在加载时工作,并正确加载 map 。但是当我改变event_type
时带有下拉菜单,它永远不会进入函数内部。我检查了concole.log
。
实际event_type
确实发生了变化,它在屏幕上显示新值 {{ event_type.type }}
$scope.event_type = { id: 0, type:'All' }
$scope.$watch( 'event_type', function(val) {
$scope.markers = Events.venues(val.type)
}, true)
最佳答案
我认为这叫做深度潜水或深度观察。无论如何,它是 $watch 上您想要设置为 true 的第三个参数。
您还可以传入新值作为参数,而不是从 event_type 读取。如下所示。
$scope.$watch( 'event_type', function(newVal) {
$scope.markers = Events.venues(newVal.type)
}, true);
这是一个骗子:http://plnkr.co/edit/YMmVPKDMeUcK5Pvz2Z0z?p=preview
如果这里的内容没有意义,请告诉我。
关于javascript - $watch 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27069992/