require:'^msgpallete'
angular.module('myApp',[])
.controller('MainCtrl', function($scope, $timeout) {
....code irreleveant in this context....
})
.directive('msgpallete',function(){
return{
restrict:"E",
scope:{},
template:"<h4>Added"+""+" "+"</h4>"
}
})
.directive('itemCounter',function(){
return {
restrict:'E',
require:'msgpallete',
scope:{
resetter:"="
},
transclude:true,
link:function(scope,elem,attr){
scope.qty = attr.startcounter
scope.add = function(){}
scope.remove = function(){}
scope.reset = function(){}
scope.$watch();
},
template:"<a ng-transclude></a>  "+
"<button ng-click='remove();' >less</button>"+
"{{qty}}" +
"<button ng-click='add();'>more</button> "+
"<button ng-click='reset();'>submit</button>"
}
});
感谢如果您查看文档,require
参数具有其他功能。
- someDirective:要求someDirective相同的元素,并将其传递给 链接功能
- someDirective:传递someDirective控制器如果有相同的元素,以链接功能。如果不是,则传递null。
- ^someDirective:需要父元素之一的someDirective并将其传递给链接函数。
- ?someDirective:传递someDirective控制器(如果其中一个父元素可用于链接功能)。如果不是,则传递null。
由于我已经完成了这一点,并且您要求的指令可能需要明确定义控制器,这已经有一段时间了。只需定义并返回一个空的控制器就足够了。
.directive('parent', function() {
return: {
controller: function() {return {}}
}
})
如[每文档】(https://docs.angularjs.org/api/ng/service/$compile)'require'只能引用*兄弟*相同的元件上(指令 - 也许这是如果混淆来自)或* parent *。 –
@ YD1m:你认真吗?如果我的'msgpallette'在页面的顶部并且'item-counter'需要在最下面。那我怎么能这样窝呢? – dreamer
@PavelHoral:你将能够回答我的以上评论 – dreamer