这是我的HTML
<example-directive id="my_id" ng-blur="test()"></example-directive>
这是我的指示
app.directive('exampleDirective', function() {
return {
restrict: 'AE',
scope: {
label: "@",
},
template: '<span class="some_input"> <input required/></span>',
controller: function($scope, $element){
},
link: function(scope, el, attr) {
// this is not responsive when blur out
scope.test = function(){
console.log("what is up");
}
// this is responsive when blur out
el.on('blur', function(){
console.log("this is up");
});
}
}
})
当我
$("#my_id").blur()
在控制台中,仅
"this is up"
出现了
我不确定当example-directive
答案 0 :(得分:1)
当您在自定义元素上使用模糊事件时,它不会起作用。您需要在隔离范围内传递该元素,以便该指令可以通过在隔离范围内添加blur: '&ngBlur'
来访问该方法。 blur
变量允许访问指定的ngBlur
属性函数。
scope: {
label: "@",
blur: '&ngBlur'
},
然后您可以在指令blur
事件
el.on('blur', function(){
console.log("this is up");
//here we need to run digest cycle manually,
//as we are dealing from outside angular context.
$timeout(function(){ //include `$timeout` dependency in directive
scope.blur();
});
});