Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
176
rated 0 times [  178] [ 2]  / answers: 1 / hits: 134556  / 11 Years ago, tue, december 31, 2013, 12:00:00

I have an angular directive which is initialized like so:



<conversation style=height:300px type=convo type-id={{some_prop}}></conversation>


I'd like it to be smart enough to refresh the directive when $scope.some_prop changes, as that implies it should show completely different content.



I have tested it as it is and nothing happens, the linking function doesn't even get called when $scope.some_prop changes. Is there a way to make this happen ?


More From » angularjs

 Answers
52

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


[#73472] Sunday, December 29, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dominics

Total Points: 424
Total Questions: 99
Total Answers: 107

Location: South Korea
Member since Fri, Sep 11, 2020
4 Years ago
dominics questions
Wed, Apr 6, 22, 00:00, 2 Years ago
Thu, Jan 13, 22, 00:00, 2 Years ago
Fri, Sep 18, 20, 00:00, 4 Years ago
;