AngularJS,实现输入框失焦添加内容到最前,当数据重复删除原来并显示在数组最前

来源:互联网 发布:freebsd 源码 编辑:程序博客网 时间:2024/06/11 16:59
<!DOCTYPE html><html><head><meta charset="utf-8" /><title></title><script src="lib/angular.min.js"></script><script>var mod = angular.module("module",[]);mod.controller("cont",["$scope",function($scope){//定义空数组$scope.datas=[];//提交事件$scope.sub = function(){//当数组不为空时,去循环查找数组相同的元素if($scope.datas.length>0){for(var i=0;i<$scope.datas.length;i++){//判断当元素存在时删除if($scope.datas[i]==$scope.tex){$scope.datas.splice(i,1);break;}}//删除完成后添加到数组$scope.datas.unshift($scope.tex);//在敲击回车后让输入框置空$scope.tex="";}else{alert("输入不能为空");//添加输入的内容$scope.datas.unshift($scope.tex);$scope.tex="";}}}]);</script></head><body ng-app="module" ng-controller="cont"><!--表单form中的ng-submit指令带有输入框敲击回车提交的功能--><form ng-submit="sub()"><!--//定义输入的内容--><input type="text" ng-model="tex"/></form><ul><!--//循环数组--><li ng-repeat="a in datas">{{a}}</li></ul></body></html>

阅读全文
0 0
原创粉丝点击