Angularjs watch for change in parent scope

前端 未结 5 1730
甜味超标
甜味超标 2020-12-24 05:32

I\'m writing a directive and I need to watch the parent scope for a change. Not sure if I\'m doing this the preferred way, but its not working with the following code:

5条回答
  •  旧巷少年郎
    2020-12-24 05:41

    If you want to watch a property of a parent scope you can use $watch method from the parent scope.

    //intead of $scope.$watch(...)
    $scope.$parent.$watch('property', function(value){/* ... */});
    

    EDIT 2016: The above should work just fine, but it's not really a clean design. Try to use a directive or a component instead and declare its dependencies as bindings. This should lead to better performance and cleaner design.

提交回复
热议问题