AngularJS:转换ng-repeat内部指令

时间:2021-10-06 20:17:48

I have a directive that transcludes the original content, parses it, and uses the information in the original content to help build the new content. The gist of it looks like this:

我有一个指令,它转换原始内容,解析它,并使用原始内容中的信息来帮助构建新内容。它的要点看起来像这样:

.directive('list', function() {
    return {
        restrict: 'E',
        transclude: true,
        templateUrl: '...',
        scope: true,
        controller: function($scope, $element, $attrs, $transclude) {
            var items;
            $transclude(function(clone) {
                clone = Array.prototype.slice.call(clone);
                items = clone
                    .filter(function(node) {
                        return node.nodeType === 1;
                    })
                    .map(function(node) {
                        return {
                            value: node.getAttribute('value')
                            text: node.innerHTML
                        };
                    });
            });

            // Do some stuff down here with the item information
        }
    }
});

Then, I use it like this:

然后,我像这样使用它:

<list>
    <item value="foo">bar</item>
    <item value="baz">qux</item>
</list>

This all works fine like this. The problem occurs when I try to use an ng-repeat inside the directive content, like this:

这一切都很好。当我尝试在指令内容中使用ng-repeat时会出现问题,如下所示:

<list>
    <item ng-repeat="item in items" value="{{ item.value }}">{{ item.text }}</item>
</list>

When I try to do this, there are no items. Anyone know why this wouldn't work, or if there is a better way of accomplishing the same kind of thing?

当我尝试这样做时,没有物品。任何人都知道为什么这不起作用,或者是否有更好的方法来完成同样的事情?

1 个解决方案

#1


2  

You may try:

你可以尝试:

transcludeFn(scope, function (clone) {
   iElem.append(clone);
})

For bit more details:

有关详细信息:

HTML:

HTML:

<foo data-lists='[lists data here]'>
 <li ng-repeat="list in lists">{{list.name}}</li>
</foo>

Directive:

指示:

var Foo = function() {
  return {
     restrict: 'E',
     template: '...'
     transclude: true,
     scope: { lists: '=?' }
     link: function(scope, iElem, iAttrs, Ctrl, transcludeFn) {
          transcludeFn(scope, function (clone) {
              iElem.append(clone);
          }
     }
  };
};

.directive('foo', Foo);

You should let transcludFn know which scope you are going to use within transcludeFn. And if you don't want to use isolate scope, you may also try transcludeFn(scope.$parent....)

您应该让transcludFn知道您将在transcludeFn中使用哪个范围。如果您不想使用隔离范围,您也可以尝试transcludeFn(范围。$ parent ....)

#1


2  

You may try:

你可以尝试:

transcludeFn(scope, function (clone) {
   iElem.append(clone);
})

For bit more details:

有关详细信息:

HTML:

HTML:

<foo data-lists='[lists data here]'>
 <li ng-repeat="list in lists">{{list.name}}</li>
</foo>

Directive:

指示:

var Foo = function() {
  return {
     restrict: 'E',
     template: '...'
     transclude: true,
     scope: { lists: '=?' }
     link: function(scope, iElem, iAttrs, Ctrl, transcludeFn) {
          transcludeFn(scope, function (clone) {
              iElem.append(clone);
          }
     }
  };
};

.directive('foo', Foo);

You should let transcludFn know which scope you are going to use within transcludeFn. And if you don't want to use isolate scope, you may also try transcludeFn(scope.$parent....)

您应该让transcludFn知道您将在transcludeFn中使用哪个范围。如果您不想使用隔离范围,您也可以尝试transcludeFn(范围。$ parent ....)