Is there a way to watch the value of a function expression change in an AngularJS directive? I have the following HTML and JavaScript, and the interpolation of {{editable()}}
in the template shows the value evaluates to true, while inspecting the HTML element in Chrome shows that contenteditable
is false.
Any suggestions on how I can watch the value of this function change, and update the element attr accordingly? Or is there a better way to achieve this? (I'd still like to evaluate the function.)
HTML:
<h4 editable="account.hasRole('ROLE_ADMIN')" content="doc.heading"></h4>
Javascript:
mod
.directive(
'editable',
function() {
return {
restrict : 'A',
template : '<button class="btn pull-right"><i class="icon-pencil"></i></button>{{content}} ({{editable()}})',
scope : {
'content' : '=',
'editable' : '&'
},
link : function(scope, element, attrs) {
scope.$watch('editable', function(newValue) {
element.attr('contenteditable', newValue());
});
}
};
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With