据说我们可以将指令视为对象,例如在网页上实现“计数”小部件。单击左侧标签将减少计数,而单击右侧将增加计数。
如果是这样,我们可以为它实现一个实例方法,来做getCount()
吗?
示例:
https://jsfiddle.net/htcvuer1/3/
HTML:
<div ng-app="myApp">
<count value="1"></count>
</div>
JS:
angular.module("myApp", [])
.directive("count", function() {
return {
template: "click me: <span ng-click='dec()'>decrement</span> <span class='the-count'>{{value}}</span> <span ng-click='inc()'>increment</span>",
scope: {
value: "="
},
link: function(scope, el, attrs) {
scope.dec = function() {
scope.value--;
};
scope.inc = function() {
scope.value++;
}
}
}
});