I require a custom DOM element that accepts mouse clicks and whose state is dependant on the model:-
<card ng-repeat="card in cards" x="card.x"
y="card.y" color="card.color" on-click="test_click('b')">
</card>
I am able to build a custom directive that binds to a controller's scope variables through its DOM attributes and use them to alter its view. I have this working by allowing the directive to inherit its parents scope:
app.directive('card', function ($timeout) {
return {
restrict:'E',
link:function (scope, element, attrs) {
element.addClass('card');
element.click(function(){
scope.onClick()
});
scope.$watch(attrs.x, function (x) {
element.css('left', x + 'px');
});
scope.$watch(attrs.y, function (y) {
element.css('top', y + 'px');
});
scope.$watch(attrs.color, function (color) {
element.css('backgroundColor', color);
});
}
/*
,scope:{
x:'=',
y:'=',
color:'=',
onClick: "&"
}
*/
};
});
I am able to get a mouse click event to propagate up to the controller by creating an isolated scope and doing some rewiring (by commenting the scope in above).
However, I am unable to get both behaviours working at the same time.
I presume I need to get the x variable bound to the attribute value, which is what I have tried to do. But even by trying every combination of syntax I can think of, I just can't seem to get it working.
Here is the complete case jsfiddle