ng-checked选择和点击增加dom
Posted quan134
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ng-checked选择和点击增加dom相关的知识,希望对你有一定的参考价值。
1.需求
在添加页面实现一个checkbox的选择,然后在详情页面展示时,会自动选上之前被选中的。
2.添加页面
看官最好将这个代码复制过去看看效果。
<!DOCTYPE html><html><head><meta charset="utf-8"><script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body><div ng-app="myApp" ng-controller="myCtrl" > 选择 <div ng-repeat="item in list"> <input type="checkbox" name="tagName" value="item.id" ng-click="select(item.id,$event)"> {{item.shortName}} </div> 结果:{{result}}</div><script> var app = angular.module(\'myApp\', []); app.controller(\'myCtrl\', function($scope) { //创建checkbox用的 $scope.list=[{"id":1,"shortName":"张三"},{"id":2,"shortName":"李四"},{"id":3,"shortName":"王二"}]; //存储已选 $scope.result = []; //触发事件 $scope.select = function(id,event){ console.log(event)//打印看看这是什么,有利于理解 console.log(action) var action = event.target; if(action.checked){//选中,就添加 if($scope.result.indexOf(id) == -1){//不存在就添加 $scope.result.push(id); } }else{//去除就删除result里 var idx = $scope.result.indexOf(id); if( idx != -1){//不存在就添加 $scope.result.splice(idx,1); } } }; }); </script></body></html>
3.详情展示
//假设添加页面的结果是:$scope.result = [3,2];
<!DOCTYPE html><html><head><meta charset="utf-8"><script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body> <div ng-app="myApp" ng-controller="myCtrl" > 回写时设置不可选,即设ng-disabled="true" <div ng-repeat="item in list"> <input type="checkbox" name="tagName" ng-checked="isSelected(item.id)" value="item.id" ng-disabled="true" > {{item.shortName}} </div> 结果:{{result}}</div> <script> var app = angular.module(\'myApp\', []); app.controller(\'myCtrl\', function($scope) { //创建checkbox用的 $scope.list=[{"id":1,"shortName":"张三"},{"id":2,"shortName":"李四"},{"id":3,"shortName":"王二"}]; //在添加页面得到的结果 //你会发现,顺序也不会影响结果 $scope.result = [3,2]; //被选中条件:ng-checked的结果为true $scope.isSelected = function(id){ return $scope.result.indexOf(id)!=-1; //只要返回的结果为true,则对应的checkbox就会被选中, //所以我的思路是看存添加页面的结果里是否含有当前id即value值, //有就返回的true,没有就返回false }; }); </script></body></html>
div.form-group label.control-label.col-md-3 发票类型 div.col-md-6 label.checkbox.pull-left(ng-repeat="(key,type) in globalConfig.invoice_type" style="margin-left:30px") input(type="checkbox" name="invoice_type" ng-model="invoice_type[key]" ng-true-value="true" ng-false-value="" ng-click="") | {{type}} //- select.form-control(ng-options="key as value for (key,value) in globalConfig.invoice_type" ng-model="info.list.invoice_type" multiple="true") //- option(value=\'\') (发票类型可多选) div.form-group label.control-label.col-md-3 发票内容 div.col-md-6 .form-group span.btn.btn-danger(ng-click="addFn(sendData.list)") 添加 div(ng-repeat="spec in sendData.list") .input-group input.form-control(type="text" ng-model="spec.content" name="brand_name" ) .input-group-addon(ng-click="removeFn( sendData.content,spec)") i.glyphicon.glyphicon-remove
angular.module(\'laoyou\')
.controller(\'invoiceCtrl\', [\'$scope\',\'$http\',\'$stateParams\',\'api\',function($scope, $http, $stateParams,api){
$scope.sendData = {list:[]};
$scope.addFn=function(list,item){
// console.log(list)
// [{type: 2, content: "ert"}, {type: 2, content: "fgh"}]
list.push({type:2});
/*if(list == undefined){
list = [];
}else{
list.push({});
}*/
}
$scope.removeFn=function(list,item){
var index=list.indexOf(item);
list.splice(index,1)
}
// 初始化invoice_type
$scope.invoice_type={};
$scope.sendFn = function () {
sendData=angular.copy($scope.sendData);
// sendData= {};
// sendData.content = $scope.sendData.content;
var type= [{"content":"普通发票","type":1},{"content":"增值税专用发票","type":1},{"content":"电子发票","type":1}];
// sendData.type=[];
var invoice=$scope.invoice_type;
console.log(invoice)
for(var x in invoice){
if(invoice[x]){
sendData.list.push(type[x-1]); //传入想要的数
}
}
console.log(sendData);
/*var info = {};
info.list = [sendData];*/
// console.log(info);
$http.post("invoice/save", sendData).success(function (data) {
if (data.statusCode === \'0\') {
alert("提交成功");
/*if ($scope.pageModel === \'add\') {
$state.go("home.employee.view", {
id: data.data
})
}else{
$state.go("home.employee.view", {
id: $stateParams.id
})
}*/
}
})
}
}])
以上是关于ng-checked选择和点击增加dom的主要内容,如果未能解决你的问题,请参考以下文章