【问题标题】:AngularJS directives - directives on transcluded contentAngularJS 指令 - 嵌入内容的指令
【发布时间】:2014-01-15 06:54:15
【问题描述】:

http://plnkr.co/edit/XbgMa9681GkKPeodplJx?p=preview

请参阅上面的 plunker。有没有办法将模板 dom 中的指令绑定到父指令的范围?我也看到了 ng-transclude 的这种行为,上面的内容是我能做到的最简单的。我读过嵌入的内容位于指令范围的兄弟范围内,但 $$nextSibling 似乎也不是诀窍。

【问题讨论】:

    标签: javascript angularjs scope directive


    【解决方案1】:

    使用指令:

    app.directive('accordion', function($compile) {
      return {
          restrict: 'C',
          scope: { 
    
          },
          link: function(scope, elem, attrs){
            scope.showSection = false;
            elem.find('p').attr('ng-show', 'false');
            $compile(elem.find('p'))(scope)
            elem.find('h1, h2').bind('click', function () {
              scope.$apply(function(){
                scope.showSection = !scope.showSection; 
                elem.find('p').attr('ng-show', 'showSection');
                $compile(elem.find('p'))(scope);
              });
            });
          }
      };
    });
    

    plunkr

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-29
      • 2015-09-18
      • 2014-09-06
      • 1970-01-01
      相关资源
      最近更新 更多