2015-10-20 17 views
0

這裏是我的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

回答

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(); 
    }); 
}); 
+0

對不起,我不太明白,'el.on( 「模糊」,函數(){的console.log內(「already」);}'當我調用一個模糊事件時工作,我只是好奇爲什麼一個範圍內的函數無法捕捉模糊事件,我們是否說如果我想讓一個範圍聽一個模糊事件它將不得不在'el.on'? – Jal

+0

@Jal你有它的方式功能是不是在指令隔離範圍它是在父範圍 – charlietfl

+0

啊,我們也說'scope.test'是在父範圍o對指令不可用? – Jal