Angular Directive refresh on parameter change

后端 未结 5 1211
不思量自难忘°
不思量自难忘° 2020-12-04 13:08

I have an angular directive which is initialized like so:

         


        
5条回答
  •  青春惊慌失措
    2020-12-04 13:56

    Link function only gets called once, so it would not directly do what you are expecting. You need to use angular $watch to watch a model variable.

    This watch needs to be setup in the link function.

    If you use isolated scope for directive then the scope would be

    scope :{typeId:'@' }

    In your link function then you add a watch like

    link: function(scope, element, attrs) {
        scope.$watch("typeId",function(newValue,oldValue) {
            //This gets called when data changes.
        });
     }
    

    If you are not using isolated scope use watch on some_prop

提交回复
热议问题