<!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>
angular input回车事件
最新推荐文章于 2025-05-23 09:00:06 发布