Javascript:
app.directive('myEnter', function () {
return function (scope, element, attrs) {
element.bind("keydown keypress", function (event) {
if(event.which === 13) {
scope.$apply(function (){
scope.$eval(attrs.myEnter);
});
event.preventDefault();
}
});
};
});
HTML:
<div ng-app="" ng-controller="MainCtrl">
<input type="text" my-enter="doSomething()">
</div>
本文介绍了如何在JavaScript中利用自定义指令`myEnter`来响应键盘事件,具体实现了当按下回车键时触发特定函数`doSomething()`的执行。
2197

被折叠的 条评论
为什么被折叠?



