赞
踩
- <!DOCTYPE html>
- <html>
- <head lang="en">
- <meta charset="UTF-8">
- <title></title>
- <script src="js/angular.js"></script>
- </head>
- <body ng-app="myApp" ng-controller="myctrl">
- <input type="text" ng-model="textmodel" ng-keyup="mykey($event)"/>
- <ul>
- <li ng-repeat="x in list1 ">
- {{x}}
- </li>
- </ul>
- <script>
- var app = angular.module('myApp', []);
- app.controller('myctrl', function ($scope) {
- $scope.list1 = ['1', '2', '3'];
- $scope.mykey = function (e) {
- var keycode = window.event ? e.keyCode : e.which;//获取按键编码
- if (keycode == 13) {
- $scope.myClick();//如果等于回车键编码执行方法
- }
- }
- $scope.myClick = function(){
- if(!arrindex($scope.list1,$scope.textmodel)){//不重复添加
- $scope.list1.push($scope.textmodel);
- }
- };
- })
- function arrindex(arr, obj) {//判断是否重复
- var i = arr.length;
- while (i--) {
- if (arr[i] === obj) {
- return true;
- }
- }
- return false;
- }
- </script>
- </body>
- </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。