面试题要求如下所示
1、angular:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>document</title> <script src="angular-1.4.6.js"></script> <style> .del{ text-decoration: line-through; color: red; } .in1{ margin-left: 40px; } </style> </head> <body ng-app="app" ng-controller="my-ctrl"> <input type="text" ng-model="val"> <button ng-click="add()">添加</button> <ul> <li ng-repeat="(key,item) in items" ng-show="flag||!items[key].labs" ng-class={true:'del',false:'unselected'}[items[key].labs]><input type="checkbox" ng-click="labe()" ng-model="lab">{{item.text}}<input type="button" value="删除" ng-click="delate()" class="in1"></li> </ul> <button type="button" ng-click="showall()">已完成开关显示</button> <button type="button" ng-click="delateall()">清除已完成</button> </body> <script type="text/javascript"> var myapp = angular.module(app,[]); myapp.controller(my-ctrl,function($scope){ $scope.items = []; $scope.flag = 1; $scope.add=function(){ $scope.items.unshift({text:$scope.val,labs:0}); } $scope.delate=function(){ $scope.items.splice(this.$index,1); } $scope.labe=function(){ $scope.items[this.$index].labs=this.lab; } $scope.showall=function(){ if($scope.flag == 0){ $scope.flag = 1; } else{ $scope.flag = 0; } } //倒序删除数组元素 //这里必须使用倒叙删除数组因为angular数据双向绑定,正序的话会导致数据随时更新影响for循环 $scope.delateall=function(){ for(var i=$scope.items.length-1;i>=0;i--){ if($scope.items[i].labs==true){ $scope.items.splice(i,1); } } } //delateall除了这种方式书写还有另外一种正序删除的方式 //$scope.delateall=function(){ //$scope.delall=[]; //for(var i=0;i<$scope.items.length;i++){ //if($scope.items[i].labs == true){ //console.log(i); //$scope.delall.push(i); //} //} //console.dir($scope.delall); //for(var j=0;j-1;i--){ if(this.alll[i].check1==true){ this.alll.splice(i,1); } } // for(let i = 0;i
