Reputation: 7773
I am trying to create a custom checkbox directive in Angular, and I have managed to do so. but the problem is that the build-in ng-click
is not called. if the custom directive is removed, the ng-click
works well. I guess it is something to do with Scope, which I am still lacking knowledge to figure out. can anybody help me please.
the code is here http://jsfiddle.net/bingjie2680/UzM2c/2/.
<div ng-app="components">
<div ng-controller="MainCtrl">
<input type="checkbox" ng-model="p.checked" />
<div checkbox="p.checked" ng-click="test()">test</div>
{{p.checked}}
</div>
</div>
js:
function MainCtrl($scope){
$scope.p = {checked: false, name:'test'};
$scope.$watch('p', function(){
}, true);
$scope.test = function(){
alert('test');
}
}
angular.module('components', []).
directive('checkbox', function () {
return {
link : function (scope, ele, attr){
ele.bind('click', function(){
ele.toggleClass('active');
scope.$apply(function(){
scope.value = ele.hasClass('active');
});
});
scope.$watch('value', function(newv, oldv){
newv ? ele.addClass('active') : ele.removeClass('active');
});
},
scope : {
value: '=checkbox'
}
}
});
Upvotes: 0
Views: 1028
Reputation: 364727
Although @Yahya's answer will work, instead of using $parent
, which can break if your HTML structure changes (i.e., HTML changes can change how scopes are inherited), you should instead specify in an HTML attribute which method/callback you want your directive to call when the element is clicked:
<div checkbox="p.checked" cb="test()">test</div>
Then in your directive:
ele.bind('click', function(){
ele.toggleClass('active');
scope.cb(); // calls test()
scope.$apply(function(){
scope.value = ele.hasClass('active');
});
});
...
scope : {
value: '=checkbox',
cb: '&',
}
Upvotes: 8
Reputation: 2481
That's because you have a directive with an isolated scope try this:
<div checkbox="p.checked" ng-click="$parent.test()">test</div>
That will work.
Hope this helped.
Upvotes: 3