簡體   English   中英

如何動態地向元素添加ng-repeat屬性

[英]How to dynamically add an ng-repeat attribute to an element

是否可以動態地向元素添加ng-repeat屬性? 如果是這樣,怎么樣?

編輯

我試圖創建一個組件,在其列表中的每個項目中使用其中的自定義模板。

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

然后結果應該是

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

所以我試着將ul的innerHTML設置為一個字符串,如下所示:

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

解決方案是顯示一個具有ng-repeat的元素,具體取決於您可以動態更改的條件:

<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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM