简体   繁体   English

Angular 1:嵌入的重复指令之间的通信?

[英]Angular 1: communications between transcluded repeated directives?

I faced with a problem.我遇到了一个问题。

I want to create tabs with separated navigation and content (each one is a separate directive), they are talking to each other using events.我想创建具有独立导航和内容的选项卡(每个选项卡都是一个单独的指令),它们使用事件相互交谈。

So, the navigations directive is pretty easy, I didn't include this part here.所以,导航指令非常简单,我没有在这里包含这部分。 But the content is much more difficult to me.但内容对我来说要困难得多。 I want to show only a selected tab and hide others (based on some variable in parent tabs ' scope).我只想显示选定的选项卡并隐藏其他选项卡(基于父tabs范围中的某些变量)。 Each tab can contain directive/expression/plain html.每个选项卡都可以包含指令/表达式/纯 html。 I tried plenty of variants but they didn't work.我尝试了很多变体,但它们不起作用。

Could you please help me to find a solution?你能帮我找到解决办法吗?

  1. How to pass a variable {{item.key}} to an attribute for ng-repeated ng-transcluded directives?如何将变量{{item.key}}传递给 ng-repeated ng-transcluded 指令的属性? Like <tab key="{{item.key}}" repeat="item in items">喜欢<tab key="{{item.key}}" repeat="item in items">

  2. How to access a variable selectedTabKey (that is in tabs directive) from every child tab's scope?如何从每个子选项卡的范围访问变量selectedTabKey (在tabs指令中)?

 var app = angular.module("app", []); var $ = angular.element; app.run(function($templateCache, $rootScope) { $rootScope.items = [ {key:1,value:'a'}, {key:2,value:'b'}, {key:3,value:'c'} ]; }); app.directive( "tabs", function() { return { restrict: "AE", scope: true, transclude: true, replace: true, template: "<div ng-transclude></div>", link: function(scope, elem, attr, ctrl) { scope.selectedTabKey = 2; // TODO: changing the selectedTabKey value // want to show/hide the related tab } }; }); app.directive( "tab", function($compile) { return { restrict: "AE", transclude: true, replace: true, template: "<div><span transclude></span></div>", link: function( scope, elem, attr, ctrl, transclude ) { var transc = $(elem[0].querySelector( "[transclude]" )); transclude( scope, function( childs ) { transc.append( childs ); }); transc.attr("ng-repeat", attr.repeat); $compile(transc)(scope); // todo: access parent's var `selectedTabKey` // and make visible the related tab } }; });
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.min.js"></script> <div ng-app="app"> <tabs> <!-- how to pass the attr {{item.key}} below? --> <tab key="{{item.key}}" repeat="item in items"> <!-- here some directive/expression/plain html --> {{item.value}} </tab> </tabs> </div>

Friend, You can't see that variable "selectedTabKey" in "tab" link because of cycle digest, you have two alternatives:朋友,由于循环摘要,您在“tab”链接中看不到变量“selectedTabKey”,您有两种选择:

  1. Put "tabs" in a pre link to run before.将“标签”放在要运行的预链接中。

    link: { pre:function(scope, elem, attr, ctrl) { scope.selectedTabKey = 2; // TODO: changing the selectedTabKey value // want to show/hide the related tab } }

OR或者

  1. Put creation of element in next digest cycle using a $timeout使用 $timeout 在下一个摘要周期中创建元素

    app.directive( "tab", function($compile, $timeout) { ... link: function( scope, elem, attr, ctrl, transclude ) { ... $timeout(function(){ console.log(scope.$parent.selectedTabKey) },0) } }

Found a solution.找到了解决办法。

Used a require in directive's configs.在指令的配置中使用了require

Here is an example: https://plnkr.co/edit/IGrgb5GY5legl2ncrw2d?p=preview这是一个例子: https : //plnkr.co/edit/IGrgb5GY5legl2ncrw2d?p=preview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM