angularJs初学遇到的坑之directive
上代码:
js部分
angular.module('indexApp',[])
.directive('increasea',function(){
return function (scope,element,attr){
element.on("click",function(){
scope.a++;
scope.$digest();
});
};
})
.controller('indexContrl',function($scope){
$scope.a = 1;
$scope.$watch('a',function(newVal){
console.log('a:'+newVal);
});
});
```html部分:
<div class="se-preview-section-delimiter"></div>
<div ng-controller="indexContrl">
<button increasea>点击</button>
<span>点击次数:</span><span ng-bind="a"></span>
</div>
“`
其中directive的名字必须小写,increasea,如果是increase-a出错,increaseA也出错!!都是坑啊,孩纸们。