angularjs中響應回車事件


下面這個示例在輸入框鍵入回車鍵或者點擊按鈕時,將輸入框的值置為"Hello World!":(黃色背景內容為響應回車事件涉及到的代碼)

<html ng-app="myApp">
<head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title>angularjs-demo</title>
<script type="text/javascript" src="lib/angularjs/angular.min.js" charset="utf-8"></script>
</head>
<body ng-controller="ctrl">
<input type="text" ng-model="text1" ng-keyup="enterEvent($event)" />
<button ng-click="clickEvent()">test</button>
</body>
<script type="text/javascript">
var app = angular.module('myApp', [])
.controller('ctrl', function($scope){
    $scope.clickEvent = function() {
        $scope.text1 = "Hello world!";
    }
    
 $scope.enterEvent = function(e) { var keycode = window.event?e.keyCode:e.which; if(keycode==13){ $scope.clickEvent(); } }
    
});
</script>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM