Child directive not updated when parent directive property changes

前端 未结 1 744
无人及你
无人及你 2021-01-26 08:56

This is follow up to these 2 questions:

  1. Pass argument between parent and child directives
  2. Parent directive controller undefined when passing to child dire
相关标签:
1条回答
  • 2021-01-26 09:48

    Solution 1

    i've set up a working plnkr here: https://plnkr.co/edit/fsxMJPAc05imhBqefaRk?p=preview

    the reason of this behaviour is that tmpMenuLink kept a copy of the value returned from MyDirectiveCtrl.isDisabled(). no watcher is set up , so the only way to resolve this is to manually watch for any changes and then update the field.

    scope.$watch(function(){
      return MyDirectiveCtrl.isDisabled();
    }, function(){
       scope.disabled = MyDirectiveCtrl.isDisabled();
    })
    

    Solution 2

    An alternative without watchers is to pass the reference of an object instead of a primitive type, something like:

    $scope.menuStatus = {status: false};
    

    new plnkr here: https://plnkr.co/edit/RGEK6TUuE7gkPDS6ygZe?p=preview

    0 讨论(0)
提交回复
热议问题