2015-05-29 111 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

相关问题