2017-01-20 44 views
0

所以我有一個字典數組。在字典中,它們具有以下特性:如何使用ng-repeat來索引動態生成的屬性列表?

的AttributeName 的AttributeValue 產品名稱

但對於每個字典,我有屬性名稱動態生成。這爲每個屬性附加索引(AttributeName0,AttributeName1等) 我這樣做的原因是因爲我使用的是ui-grid,並且它需要列名是唯一的,以便爲每個字典創建一個新列在

字典看起來是這樣的(在一個名爲resultsToSend數組): enter image description here

我試圖用NG-重複使用這些字典動態創建一個表單:

<div class="row" ng-repeat="result in resultsToSend"> 
         <div class="col s2"> 

          <md-input-container><label>{{result.AttributeName$index}}</label><input type="number" id="new-day-ARP-input" ng-model="result.AttributeName$index" /></md-input-container> 
         </div> 
         <div class="col s4"> 
          <label>Select {{result.ProductName$index}} Material</label> 
          <md-select ng-model="result.ProductName$index}" md-on-close="clearSearchTerm()" data-md-container-class="selectdemoSelectHeader"> 
           <md-select-header class="demo-select-header"> 
            <input ng-model="searchTerm" type="search" placeholder="Search For Material..." class="demo-header-searchbox md-text"> 
           </md-select-header> 
           <md-option ng-value="{{mat.ID}}" ng-repeat="mat in Materials | filter:searchTerm">{{mat.Name}}</md-option> 
          </md-select> 
         </div> 
        </div> 

但我一直沒有成功找到我們如何(PropertyNameHere)$ index來訪問我試圖訪問的屬性。

有沒有人有任何建議?

+0

如何添加新屬性?通過推或什麼? – Jenny

+0

'var tempProductName =「ProductName」+ String(k); var tempAttributeName =「AttributeName」+ String(k); var tempAttributeValue =「AttributeValue」+ String(k); var tempArray = []; tempArray [tempProductName] =''; tempArray [tempAttributeName] = response.data [0] .Attributes [k] .Name; tempArray [tempAttributeValue] ='' $ scope.resultsToSend.push(tempArray);' – user44036

+0

你不能訪問**結果**作爲一個對象,如:result。(PropertyNameHere)$ index,因爲結果是不可用的範圍有resultsToSend存在,並且結果用於通過resultToSend循環,因此使用它來訪問它:* * resultsToSend [$ index] .ProductName ** – Jenny

回答

1

請在您的md-select標籤中使用ng-model中的{{result.ProductName + $index}}

+0

啊,我之前就試過這樣做,但我意識到如果我試圖像這樣分配ng模型,它會引發錯誤「Error:[$ parse:syntax] Syntax Error :令牌'。'在[.ProductName + $ index]開始的表達式[{result.ProductName + $ index]的第8列處,出乎意料,期待[}]。「所以我認爲這本來是做錯的方法。 – user44036

+0

你試過換點[]?像這樣:{{result [ProductName] + $ index}}? –

+0

它給了我:angular.js:13920錯誤:[ngModel:nonassign]表達式結果[AttributeName] + $ index'是不可分配的。元素: user44036

相關問題