2015-05-29 169 views
0

我將控制器$ scope函數傳遞給通過html屬性的指令,但由於某種原因,指令認爲函數是字符串。任何提示?

HTML

<modal show='createCustomer' create-new-customer='createNewCustomer()'></modal> 

指令

function modalDialog() { 
    return { 
    restrict: 'AE', 
    scope:{ 
     createNewCustomer: '&' 
    }, 
    replace: true, 
    transclude: true, 
    link: function(scope, element, attrs) { 
     scope.createNewCustomer = attrs.createNewCustomer; 
     console.log(typeof scope.createNewCustomer) 
    }, 
    templateUrl: "./views/directive_templates/modal.html" 
    }; 
} 

$範圍功能

$scope.createNewCustomer = function(){ 
    alert('yo') 
    } 

最佳, 奧斯汀

+0

不要將其設置成你的紐帶作用,把它在你的'範圍{}'選項已經創造我關於你的指令的範圍 –

回答

0

你的代碼應該是:

<modal show='createCustomer' create-new-customer='createNewCustomer'></modal> 

這是最有可能立即調用該函數,並在結果經過

0

在你的指令,你已經有scope.createNewCustomer綁定功能,通過在隔離範圍內使用&參數。但是,當您通過attrs重新分配函數時,實際上會用字符串值覆蓋該函數。 attrs只是一組鍵值對,它們是元素中每個屬性的字符串表示形式。

0

AngularJs指令允許您在範圍中使用'&'方法,有兩種方法。第一種方法可讓您在您的獨立指令中傳遞參數。第二種方法允許您在指令外傳遞參數,但要調用INSIDE ISOLATED DIRECTIVE.Like

<div my-dir func="myFunc(arg1,arg2,..)"></div> 
<div my-dir func="myFunc(value1,value2,..)"></div> 

如果你想要更多的在這裏是一個很酷的文章舉例:

http://www.w3docs.com/snippets/angularjs/angularjs-directive-scope-method.html

相關問題