我想将一些数据从HTML传递给指令,也可以通过ng-click点击此指令。 问题是我如何将链接功能的参数传递给模板?
app.directive("hello", function(){
return {
restrict: "E",
template : '<button ng-click="updateModel()">'+attrs.name+'</button>',
scope : {},
link : function(scope, element, attrs, ctrl){
scope.updateModel = function(){
console.log("yay");
}
}
};
});
&#13;
这是HTML指令:<hello name="john"></hello>
答案 0 :(得分:2)
试试这个方法
JS
app.directive("hello", function(){
return {
restrict: "E",
template : '<button ng-click="updateModel()" >{{name}}</button>',
scope : {
name :"@",
user :"="
},
link : function(scope, element, attrs, ctrl){
scope.updateModel = function(){
console.log(scope.name, scope.user);
}
}
};
});
HTML
<hello name="jimbrooism" user="user"></hello>
答案 1 :(得分:1)
外观:
app.directive("hello", function(){
return {
restrict: "E",
template : '<button ng-click="updateModel()">{{name}}</button>',
scope : {
name: "@"
},
link : function(scope, element, attrs, ctrl){
scope.updateModel = function(){
console.log("yay");
}
}
};
});