I am trying to style an input type='checkbox' with an angular directive and my HTML layout looks like this
<div class="checkbox"><input type="checkbox" ng-model="checked1" /></div>
<div class="checkbox"><input type="checkbox" ng-model="checked2" /></div>
<div class="checkbox"><input type="checkbox" ng-model="checked3" /></div>
My directive so far looks like this:
myApp.directive('checkbox', function($parse) {
return {
restrict: "C",
compile: function(elem, attr) {
var model = $parse(attr.ngModel);
return function(scope, elem, attr) {
var toggleValue = function() {
scope.$apply(function(scope) {
model.assign(scope, !model(scope));
});
};
var updateComponent = function(value) {
if(value == true)
elem.addClass('checkbox-active');
else
elem.removeClass('checkbox-active');
};
elem.bind('click', toggleValue);
scope.$watch(model, updateComponent);
};
}
}
});
The problem with this directive is that it searched the ng-model from the not from the input inside the div so it would work if I had the layout like this
<div class="checkbox" ng-model="checked1"><input type="checkbox" /></div>
Can I change something into the compile of the directive so I make it read the ng-model from the input rather than from the div ?