在指令中使用ng-transclude ng-repeat

Jan*_*Jan 45 javascript angularjs

我想在内容更改时创建一个包含自定义行为的列表.我尝试为此创建一个指令但是我对如何将ng-transclude与ng-repeat指令结合起来感到有点迷茫.有人能让我走上正轨吗?

HTML:

<div ng-app="myApp">
  <div ng-controller="ctrl">
    <mylist items="myItem in items">
       <span class="etc">{{myItem}}</span>
    </mylist>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

使用Javascript:

angular.module('myApp', [])    

.controller('ctrl', function ($scope) {
  $scope.items = ['one', 'two', 'three'];
})    

.directive('mylist', function () {
  return {
    restrict:'E',
    transclude: 'element',
    replace: true,
    scope: true,
    template: [
      '<ul>',
        '<li ng-repeat="WhatGoesHere in items" ng-transclude></li>',
      '</ul>'
    ].join(''),
    link: function (scope, element, attr) {
      var parts = attr.items.split(' in ');
      var itemPart = parts[0];
      var itemsPart = parts[1];
      scope.$watch(itemsPart, function (value) {
        scope.items = value; 
      });      
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

我有一部分工作在这里

编辑:

标准:

  • 项目的模板必须在视图中定义,而不是在指令中定义,并且必须能够访问子范围中的项目属性.理想情况下,我想定义它,就像在ng-repeat指令中完成一样
  • 该指令必须能够访问该列表,以便我可以设置适当的手表并更改内容.如果可能的话,我希望能够轻松访问生成的DOM项目(我也可以使用element[0].querySelectorAll('ul>li')或者某些东西,它只需要在Chrome上运行).
  • 如果可能的话,我想重用ng-repeat指令中的逻辑,因为它已经做了很多我想要的事情.我最好不要复制代码.我只想增加它的行为,而不是改变它

Jan*_*Jan 20

自己解决了这个问题:

我可以在编译步骤(jsfiddle)中通过在编译ng-repeat模板时添加属性并将其提供给我的属性的内容来完成.

HTML:

<div ng-app="myApp">
  <div ng-controller="ctrl">
    <mylist element="myItem in items">{{myItem}}</mylist>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

使用Javascript:

var myApp = angular.module('myApp', [])

.controller('ctrl', function ($scope) {
  $scope.items = ['one', 'two', 'three'];
})

.directive('mylist', function ($parse) {
  return {
    restrict:'E',
    transclude: 'element',
    replace: true,
    scope: true,
    template: [
      '<ul>',
      '<li ng-transclude></li>',
      '</ul>'
    ].join(''),
    compile: function (tElement, tAttrs, transclude) {
      var rpt = document.createAttribute('ng-repeat');
      rpt.nodeValue = tAttrs.element;
      tElement[0].children[0].attributes.setNamedItem(rpt);
      return function (scope, element, attr) {
        var rhs = attr.element.split(' in ')[1];
        scope.items = $parse(rhs)(scope);
        console.log(scope.items);
      }        
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

  • 这在当前的角度版本中不再起作用.请参阅此讨论:https://github.com/angular/angular.js/issues/7874 (2认同)

Izh*_*aki 14

实现此目的的另一种方法如下.

index.html的:

<html ng-app='myApp'>

<head>
    <title>AngularJS Transclude within Repeat Within Directive</title>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.18/angular.min.js"></script>
    <script src='index.js'></script>
</head>

<body ng-controller='myController'>
    <people>Hello {{person.name}}</people>
    <button name="button" ng-click="changeRob()">Change Rob</button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

index.js:

var myApp = angular.module( 'myApp', [] );

myApp.controller( 'myController', function( $scope ) {
    $scope.people = [
        { name: 'Rob'  },
        { name: 'Alex' },
        { name: 'John' }
    ];

    $scope.changeRob = function() {
        $scope.people[0].name = 'Lowe';
    }
});

myApp.directive( 'people', function() {
    return {
        restrict: 'E',

        transclude: true,
        template: '<div ng-repeat="person in people" transcope></div>',
    }
});

myApp.directive( 'transcope', function() {
    return {
        link: function( $scope, $element, $attrs, controller, $transclude ) {
            if ( !$transclude ) {
                throw minErr( 'ngTransclude' )( 'orphan',
                    'Illegal use of ngTransclude directive in the template! ' +
                    'No parent directive that requires a transclusion found. ' +
                    'Element: {0}',
                    startingTag( $element ));
            }
            var innerScope = $scope.$new();

            $transclude( innerScope, function( clone ) {
                $element.empty();
                $element.append( clone );
                $element.on( '$destroy', function() {
                    innerScope.$destroy();
                });
            });
        }
    };
}); 
Run Code Online (Sandbox Code Playgroud)

在这个类似的plunker中看到它的实际效果.基于这个长期的Github问题讨论.


Mor*_*ani 9

不幸的是,其他答案不适用于最新版本的角度(我检查过1.4)所以我认为分享这个jsbin有一个好处我发现:

var app = angular.module('app', [])
  .controller('TestCtrl', function($scope) {
    $scope.myRecords = ['foo', 'bar', 'baz'];
  });

app.directive('myDirective', function($compile) {
  var template = '<div id="inner-transclude" ng-repeat="record in records"></div>';

  return {
    scope: {
      records: '='
    },
    restrict: 'A',
    compile: function(ele) {
      var transclude = ele.html();
      ele.html('');

      return function(scope, elem) {
        var tpl = angular.element(template);
        tpl.append(transclude);

        $compile(tpl)(scope);

        elem.append(tpl);
      };
    }
  };
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.js"></script>


<div ng-app="app" ng-controller="TestCtrl">
  <div my-directive records="myRecords">
    ?: {{record}}
  </div>

</div>
Run Code Online (Sandbox Code Playgroud)


Mar*_*cok 5

Transcluding不是必需的,因为它items包含了渲染模板所需的内容.换句话说,元素内部没有任何东西 - 即 <mylist>nothing new here we need to transclude</mylist>.似乎Angular也会为我们收看.

.directive('mylist', function () {
  return {
    restrict:'E',
    replace: true,
    scope: true,
    template: [
      '<ul>',
      '<li ng-repeat="myItem in items">{{myItem}}</li>',
      '</ul>'
    ].join('')
  }
});
Run Code Online (Sandbox Code Playgroud)

HTML:

<mylist></mylist>
Run Code Online (Sandbox Code Playgroud)

小提琴.

请注意,创建新范围是可选的,因此您可以注释掉这一行:

//scope: true,
Run Code Online (Sandbox Code Playgroud)

更新:您可以选择创建隔离范围:

scope: { items: '='},
Run Code Online (Sandbox Code Playgroud)

HTML:

<mylist items=items></mylist>
Run Code Online (Sandbox Code Playgroud)

小提琴.

Update2:根据Jan提供的其他信息:

必须在视图中定义项的模板...我想重用ng-repeat指令中的逻辑

好吧,让我们把它全部放在视图中,并使用ng-repeat:

<ul mylist>
  <li ng-repeat="myItem in items">
    <span class="etc">{{myItem}}</span>
   </li>
</ul>
Run Code Online (Sandbox Code Playgroud)

它[指令]必须能够访问子作用域中的item属性...该指令必须能够访问该列表,以便我可以设置适当的监视和更改内容

继原来的小提琴,我们将使用一个正常的孩子范围(即子范围将prototypically从父范围继承)scope: true,.这将确保指令可以访问控制器范围上定义的属性,例如items.

访问生成的DOM项目

指令的link函数有一个element参数.所以在上面的HTML中,元素将被设置为<ul>元素.所以我们可以访问所有DOM元素.例如,element.find('li')element.children().在下面引用的小提琴中,我有$观看items数组.$ watch回调可以访问element,因此您可以访问生成的DOM项.回调记录element.children()到控制台.

小提琴.

总而言之,要将自定义行为添加到列表中,只需将指令放到ul或ol上即可.

  • 你不明白,我的项目模板`<mylist> </ mylist>`比我在这里给出的例子复杂得多 (2认同)