第三种绑定函数换成第一种传入不同函数名后在link中定义不同函数可以吗以及区别?
&绑定用于传递父scope函数,也就是说调用了controller的函数。我想其实可以从外部传入所要调用的函数名,然后函数在指令里面的link中定义不是也可以吗?但是我写的代码并没有运行成功,请大家看看代码有什么错?以及请问这种同一指令调用不同函数到底是写到控制器里面好还是写到指令里好,以及为什么?谢谢
<!doctype html>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script>
</head>
<body>
<div ng-controller="myAppCtrl">
<xingoo pd="sayHello"></xingoo>
<xingoo pd="sayNo"></xingoo>
<xingoo pd="sayYes"></xingoo>
</div>
<script type="text/javascript">
var myAppModule = angular.module("myApp",[]);
myAppModule.directive("xingoo",function(){
return {
restrict:'AE',
scope:{
say:'@pd'
},
template:'<input type="text" ng-model="username"/><br>'+
'<button ng-click="{{ say }}({name:username})">click</button><br>',
repalce:true
link : function(scope, element, attrs) {
$scope.sayHello = function(name){
console.log("hello !"+ name);
};
$scope.sayNo = function(name){
console.log("no !"+ name);
};
$scope.sayYes = function(name){
console.log("yes !"+ name);
};
}
}
})
</script>
</body>
</html>