对角度指令应用作用域并调用对象函数
applying scopes on angular directives and calling object function
此代码运行良好,并使用父控制器作用域;
"<button type='button' post-random-feed >Post review</button>"
.directive("postRandomFeed",['config',function(){
return function(scope,element.attrs){
element.bind("click", function(){
scope.name ="henry";
console.log(element);
});
}
}]);
我的问题是,我想定义一个独立的范围,并返回一个对象,而不是指令中的函数。这是我的代码
"<button type='button' post-random-feed >Post review</button>"
.directive("postRandomFeed",['config',function(){
return {
restrict :"A",
scope : {
},
irony : function(element, attrs)
{
element.bind("click", function(){
console.log(element);
});
}
};
}]);
我如何调用讽刺函数,使元素注册点击事件?如果有任何错误,请纠正我。谢谢
您可以向控制器注册回调:
html:
<button type='button' post-random-feed on-irony="onControllerFn">Post review</button>
指令:
.directive("postRandomFeed",['config',function(){
return {
restrict :"A",
scope : {},
link : function(scope, element, attrs) {
$timeout(function () {
scope.$emit(attr.onIrony);
});
});
}
};
}]);
在控制器中,您可以执行以下操作:
$scope.$on('onControllerFn', function(event) {
// your code here
});
相关文章:
- JQuery Deferred Ajax,将结果保存在调用对象中
- 无法从JavaScript中的函数调用对象属性
- 在Javascript中调用对象方法时不是函数类型错误
- 正在调用对象中设置的匿名函数,但未得到预期结果
- 为什么需要“this”来调用对象内部的函数
- 如何从onClick事件调用对象函数
- 是否可以在不加对象名称前缀的情况下调用对象的函数
- 对象-在定义对象之前先调用对象
- console.log表示,当我调用对象的任何成员时,对象都是未定义的
- 正在调用对象的方法
- console.log是否调用对象的toString方法
- Javascript”;无效的调用对象“;xml错误
- 如何在javascript中调用对象键(codecademy)
- 如果我丢失了调用对象,如何杀死 setInterval()/setTimout()
- 如何在对象生成器外部调用对象属性值
- JavaScript:动态调用对象内的函数
- 如何动态加载 JavaScript 对象或调用对象方法
- IE9 在 iframe 中打印 PDF 时出现“无效调用对象”
- 在方法调用对象之外访问变量而不使它们全局化(可能吗?
- Javascript - 在对象中调用对象的方法,给出错误:“对象没有方法getStartPoint”