最近做项目,要用到AngularJs,之前也有用过一点点,但仅限于数据的绑定,这次项目要整个前端需要使用这个框架,可能是不熟悉的原因,感觉这代码搞起来非常的不便利,;现总结一个响应回车事件:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>test</title> <script src="http://apps.bdimg.com/libs/angular.js/1.3.9/angular.min.js"></script> <script> var myApp = angular.module("myApp", []); myApp.controller("testCtrl", function($scope){ $scope.myClass = 'red'; $scope.isClick = 'No!'; $scope.myKeyup = function(e){ var keycode = window.event?e.keyCode:e.which; if(keycode==13){ $scope.myClass = 'green'; $scope.myClick(); } }; $scope.myClick = function(){ $scope.isClick = 'Yes!'; }; }); </script> <style> .red {color:red} .green {color:green} </style> </head> <body> <div ng-app="myApp"> <div ng-controller="testCtrl"> <input ng-keyup="myKeyup($event)" type="text" /> <ul> <li ng-click="myClick()" class="{{myClass}}">{{isClick}}</li> </ul> </div> </div> </body> </html>