我正在编写一个angularjs指令,它可以在整个应用程序中重复使用
<div ><div ng-controller="ctrl">
<script type="text/ng-template" id="partials/checkbox.html">
<div ng-repeat="obj in data">
<input on-check type="checkbox" ng-model="checked" value="{{obj}}" click-checkbox="checkstatus(checked,obj)" checked-me="checked" />{{obj}}</div>
</script>
<check-boxes getType="data"></check-boxes>
<check-boxes getType="bestnights"></check-boxes>
</div></div>
在属性更改时,指令应该在gettype上使用ng-repeat并使用controller和每个数组的返回对象进行迭代。
var app=angular.module('myApp',[]);
app.controller('ctrl',function($scope){
$scope.data=['Mon','Tue','Wed','Thu','Fri','Sat','Sun'];
var daysArray=[];
$scope.checkStatus=function(checked, obj){
var index=daysArray.indexOf(obj);
if(checked){
if(index === -1){
daysArray.push(obj);
} }
if(!checked){
daysArray.splice(index,1);
}
var str=daysArray.toString();
console.log(str);
console.log(checked);
};
});
app.directive('checkBoxes',function(){
return{
restrict:"EA",
controller:"ctrl",
templateUrl:"partials/checkbox.html",
link:function(scope, ele, attrs,dctrl){
ele.bind('click',function(){
});
}
};
});
app.directive('onCheck',function(){
return{
restrict:"A",
scope:{
clickCheckbox:"&",
value:"@",
checkedMe:"="
},
link:function(scope,ele, attrs){
ele.bind('click',function(){
console.log(scope.value);
});
}
};
});
我在jsbin JSBIN
做了