Reputation: 2918
I'm going to implement an element in Angular which would be able to set a value to a hidden field and change a state of the element on mouse click. Shortly, I need an image which behaves like a checkbox. To implement this element I decided to make a Angular "directive". But when I change the value of the container selection state, the 'selected' class is not added to the container. How to do this in right way with Angular?
Also I can't figure out how to update the input's value, what technique to choose here. Note, that initial state of the hidden field goes from a server side.
This is a demo of the problem: demo.
Upvotes: 2
Views: 2072
Reputation: 16644
I updated the plunker. There were three small errors:
.selected
was missing the solid
attribute for the border.You need to call $apply
in your directives click listener to update the scope:
el.on('click', function() {
scope.isSelected = !scope.isSelected;
scope.$apply();
});
You used ng-class in the wrong order:
ng-class="{selected: isSelected}"
Upvotes: 2