angular实现form验证实例代码(2)

(function(window) { 'use strict'; var mymodule = angular.module('formModule', []); // 城市删选器 mymodule.filter('cityfilter',function(){ return function(data,parent){ var cityData=[]; angular.forEach(data, function(item, key){ if(item.parent==parent){ cityData.push(item); } }); return cityData; } }); mymodule.controller('formctrl', ['$scope', function($scope) { // 设定初始状态 $scope.data={ Ahoppy:[1,3] } // 爱好对象 $scope.hoppies = [ {id: 1,name: '玩游戏',checked: istrue(1)}, {id: 2,name: '吃饭',checked: false}, {id: 3,name: '睡觉',checked: false}, {id: 4,name: '玩游戏',checked: true} ]; // 城市 $scope.cities=[ {name:'河南',parent:0,id:1}, {name:'郑州',parent:1,id:2}, {name:'郑东新区',parent:2,id:3}, {name:'金水区',parent:2,id:4}, {name:'二七区',parent:2,id:5}, {name:'信阳',parent:1,id:6}, {name:'商城',parent:6,id:7}, {name:'罗山',parent:6,id:8}, {name:'杭州',parent:0,id:9}, {name:'西湖区',parent:9,id:10}, {name:'余杭区',parent:9,id:11}, {name:'萧山区',parent:9,id:12}, {name:'上城区',parent:9,id:13}, ]; // 判断是否是选中状态 function istrue(id){ for(var i=0;i<$scope.data.Ahoppy.length;i++){ if($scope.data.Ahoppy[i]===id){ return true; } } return false; }; // 获取选中的爱好 $scope.togglehoppy = function() { $scope.data.Ahoppy = []; angular.forEach($scope.hoppies, function(item, key) { if (item.checked == true) { $scope.data.Ahoppy.push(item.id); } }); } }]) })(window)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwdjjj.html