在編譯階段,我可以訪問所有attrs元素。所以爲什麼當通過這個ATTR IM的功能我得到undefiened當傳遞到函數時,角度獲取attr undefined
這裏是代碼和plunker:
.directive('test', function() {
return {
restrict:'A',
compile: function(ele, attr) {
var div = angular.element('<div ng-show="someCond(attr.some)">directive</div>');
ele.append(div)
return function(scope, ele, attr) {
scope.someCond = function(name) {
console.log(name);
return name == 'john';
}
}
}
}
})
http://plnkr.co/edit/ne4Z24VM1VmOcwAuUdCT?p=preview
謝謝。我選擇了你的答案,因爲我不需要做額外的工作,並將屬性分配給@ OXMO456答案。 – Bazinga 2014-11-02 15:22:18
但爲什麼我需要用這種方式寫一些=「'約翰'」,當我從鏈接函數獲取attr時,我可以像這樣做一些=「那」? – Bazinga 2014-11-02 17:30:51
由於'ng-show'期望和表達。既然你需要傳遞常量表達式''''是必需的。如果我們不添加''''表達式期望值來自'$ scope.john' – Chandermani 2014-11-02 17:38:56