这段jquery代码在angular指令中不起作用
piece of jquery code is not working in angular directive
我有一个指令,里面有几个输入,当按下回车键时,我需要一个特定的输入来执行函数。
这是我执行函数所需要的输入。
<input type="text" class="form-control" ng-model="cep" id="inputcep">
我有一个jquery函数,它使用了一个掩码插件和一个keypress监听器,该监听器阻止enter键的默认操作并执行该函数。我的问题是没有调用按键,但正在应用掩码:/
$(document).ready(function () {
$('#inputcep')
.keypress(function(ev){
console.log(ev);
if(ev.keyCode == 13){
ev.preventDefault();
scope.searchCep(scope.cep);
}
})
.mask('00000-000');
});
使用angular而不是jQuery:
https://docs.angularjs.org/api/ng/directive/ngKeypress
按如下方式创建自定义指令。
指令
app.directive('customDir', function() {
return function(scope, element, attrs) {
element.bind("keydown keypress", function(event) {
element.keypress(function(ev) {
console.log(ev);
if (ev.keyCode == 13) {
ev.preventDefault();
scope.searchCep(scope.cep);
}
}).mask('00000-000');
});
};
});
相关文章:
- 新的自定义角度指令不起作用
- AngularJS,自定义指令不起作用@ plnkr
- AngularJS + Coffeescript - 'Hello World' 指令不起作用
- 使用 $provide 重命名第三方角度指令 - 不起作用
- 带有指令不起作用的 AngularJS 动态表单字段 ID
- AngularJS:双向数据绑定 - 带有ng重复的指令 - 不起作用
- 为什么我在 AngularJs 中的指令不起作用
- 剑道指令不起作用
- ng-单击指令不起作用
- 为什么这个 angularjs 指令不起作用
- 角度引导添加新指令不起作用
- AngularJS + Facebook喜欢指令不起作用
- 带有 ngModel 绑定的 AngularJS 自定义指令不起作用
- Angularjs 指令不起作用“意外令牌”
- jQuery插件中的Angular指令不起作用
- 可重复使用的按钮指令不起作用
- Angular JS自定义指令不起作用
- 带有编译函数和ng-show的角度对话框指令不起作用
- 一个元素上的多个指令不起作用
- AngularJS:drag-n-drop指令不起作用