2016-09-28 60 views
0

我構建了一個下拉指令,它使用不同的對象類型,每個對象都有自己的特定屬性,我需要在我的下拉列表的ng-repat中獲得一些特定的字段,現在,它是固定的cityName,我怎樣才能改變cityName的變量,哪些留在控制器上?angular ng-repeat內動態屬性引用

<div class="listCombo" ng-show="showDrop" ng-mouseleave="lostFocus()"> 
     <table> 
      <tr ng-repeat="result in results" ng-click="selectItem(result)"> 
       <td> {{result.cityName}} </td> 
      </tr> 
     </table> 
    </div> 

例如,我需要得到peopleName而不是的cityName

+1

我真的不明白你想要什麼,你想在'td'上設置cityName ore opepleName? –

回答

2

是有可能在ng-repeat。下面動態改變屬性屬性,關於如何實現這一點的樣品的示例代碼。

1)你應該有數據源像下面讓一切變得簡單

$scope.objectsList = [{ 
       'name': 'Vikram', 
       'Age': '25', 
       'sex': 'M' 
      }, { 
       'name': 'Quantum', 
       'Age': '26', 
       'sex': 'F' 
      }, { 
       'name': 'Katty', 
       'Age': '22', 
       'sex': 'F' 
      }]; 
      $scope.objName = 'name'; 

2)在你的HTML有這樣的事情在你的ng-repeat

<p ng-repeat="obj in objectsList"> 
    {{obj[objName]}} <!-- here by changing the 'objName' we can decide which property value to be displayed dynamically--> 

</p> 
<input type="text" ng-model="objName"/><!-- This is for example..u dont need this--> 

如果你看一下JS,我們有如果我們將$scope.objName更改爲'年齡',那麼它會在數據源中顯示相應的年齡。

希望這是你的問題的答案。

0

如果你想在TD有條件地顯示的cityName或peopleName,

你可以使用這個樣子。

它不是有條件地構建DOM,但您可以看到這樣的情況。 Wheather這不是一個好方法,但我希望它能幫助你。

<div class="listCombo" ng-show="showDrop" ng-mouseleave="lostFocus()"> 
     <table> 
      <tr ng-repeat="result in results" ng-click="selectItem(result)"> 
       <td ng-show="someCondition(result) === true"> {{result.cityName}} </td> 
       <td ng-show="someCondition(result) === false"> {{result.cityName}} </td> 
      </tr> 
     </table> 
    </div>