简体   繁体   中英

How to dynamically add an ng-repeat attribute to an element

Is it possible to dynamically add an ng-repeat attribute to an element? If so, how?

EDIT :

I was trying to create a component that will use a custom template inside it for each items on its list.

Example

<custom-component>
  <item-template>
    <span>{{item.name}}</span>
  <item-template>
<custom-component>

Then the result shall be

<custom-component>
  <ul>
    <li ng-repeat="item in $ctrl.items">(the template)</li>
  </ul>
</custom-component>

So I tried to just set the innerHTML of the ul to a string like so:

ul.innerHtml = "<li ng-repeat="item in $ctrl.items">{{item.name}}</li>"

A solution would be showing an element which have ng-repeat depending on a condition that you can change dynamically:

<div ng-switch="dynamicCondition">
    <div ng-switch-when="true" ng-repeat="item in items">Element which have ng-repeat</div>
    <div ng-switch-when="false">Element without ng-repeat</div>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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