如何公开从指令,是一个可重用组件公共API? [英] How to expose a public API from a directive that is a reusable component?
本文介绍了如何公开从指令,是一个可重用组件公共API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
具有在角一个指令,该指令是一个可重复使用的部件,什么是公开可以从控制器进行访问的公共API的最佳实践?
因此,当有组件的多个实例,您可以从控制器访问
Having a directive in angular that is a reusable component, what is the best practice to expose a public API that can be accessed from the controller? So when there are multiple instances of the component you can have access from the controller
angular.directive('extLabel', function {
return {
scope: {
name: '@',
configObj: '='
},
link: function(scope, iElement, iAttrs) {
// this could be and exposed method
scope.changeLabel = function(newLabel) {
scope.configObj.label = newLabel;
}
}
}
});
然后当有:
<ext-label name="extlabel1" config-obj="label1"></ext-label>
<ext-label name="extlabel2" config-obj="label2"></ext-label>
<ext-label name="extlabel3" config-obj="label3"></ext-label>
我怎样才能在控制器访问extLabel2的scope.changeLabel?
How can I get the access the scope.changeLabel of extLabel2 in a controller?
是否有意义?
推荐答案
这是否对你的工作?
angular.directive('extLabel', function() {
return {
restrict: 'E',
scope: {
api: '='
},
link: function(scope, iElement, iAttrs) {
scope.api = {
doSomething: function() { },
doMore: function() { }
};
}
};
});
从包含父
<ext:label api="myCoolApi"></ext:label>
和控制器中
$scope.myCoolApi.doSomething();
$scope.myCoolApi.doMore();
这篇关于如何公开从指令,是一个可重用组件公共API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文