I created a directive that utilizes different templates based on the state of the scope
as shown below:
app.directive('foo', function($compile) {
return {
restrict: 'E',
scope: {
bar: '='
},
link: function(scope, element) {
var tpl = scope.bar == 'foo' ? '<li><strong>{{bar}}</strong</li>' : '<li>{{bar}}</li>'
element.replaceWith($compile(element.html(tpl).contents())(scope))
}
}
});
When using this directive within an ngRepeat
loop and the array being used changes, the new elements get rendered but the old ones remain visible.
<ul>
<foo bar="bar" ng-repeat="bar in bars"></foo>
</ul>
Check out the live demo here: http://plnkr.co/edit/UnsrjPh8kW27bK8RbPgY
Could someone please advise me on what I might be doing wrong?