I am trying to implement a custom directive for a counter widget.
I have been able to implement it, but there are many things i need some light to be thrown on.
- Can this directive be written in a better way ?
- How do i use the
scope:
(isolate scope) in a better way ? - on click of any reset button i want all the
startnumber
to be reset to "1" ? - Where does the
scope
inherit from?Does it inherit from the element being called from?
HTML snippet
<body>
<counter-widget startnumber=1 ></counter-widget>
<counter-widget startnumber=1 ></counter-widget>
<counter-widget startnumber=1 ></counter-widget>
</body>
JS snippet
angular.module("myApp",[])
.directive("counterWidget",function(){
return{
restrict:"E",
scope:{
},
link:function(scope,elem,attr){
scope.f = attr.startnumber;
scope.add = function(){
scope.f = Number(scope.f) + 1;
}
scope.remove = function(){
scope.f =Number(scope.f) - 1;
}
scope.reset = function(){
scope.f = 1;
}
},
template:"<button ng-click='add()'>more</button>"+
"{{f}}"+
"<button ng-click='remove()'>less</button> "+
"<button ng-click='reset()'>reset</button><br><br>"
}
})
Thanks in advance for the help.