2017-03-31 99 views
0

我有一個使用下面的init方法的typecript控制器。我能夠在消息結果 中獲得我想要的所有數據,這很好。使用angularjs將數據綁定到下拉列表

  init() { 
        this.accountLocationService.getAccountLocationCollection(this.customerNumber) 
         .success(messageresult => { 
          this.acctLocationCollection = messageresult;          
          //alert(this.acctLocationCollection.accountlocation[0].aliasName); 
         }) 
         .error(error => { 
          alert(error); 
         }); 
       } 

的acctLocationCollection可以有兩個或多個對象,我需要的 的aliasname的所有值綁定在下面一個下拉。 我該如何實現這一目標?這是我的第一個AngularJs項目。

 <div id="acctDetail"> 
      <p>Select an Account:</p> 
      <div class="selectAccount"> 
       <select class="selAccounts" name="DeliverToCustomerNumber" ng-change="" id="DeliverToCustomerNumber" 
         ng-options="ac for ac in alc.acctLocationCollection.aliasName">   
       </select> 
      </div> 
      <div id="address"> 
       <dl> 
        <dd class="acctName">{{alc.acctLocationCollection.DeliverToCompanyName}}</dd> 
        <dd class="acctNo">{{alc.acctLocationCollection.DeliverToCustomerNumber}}</dd>   
       </dl> 
      </div> 
     </div> 

數據看起來像這樣的console.log

object 
    accountlocation:Array[2] 
    0:object 
     aliasName:"1234" 
     deliverToCustomerNumber: "25235" 

    1:object 
     aliasName:"2345" 
     deliverToCustomerNumber: "23523" 
+0

只是注意 - 由於您使用的打字稿,你並不需要一個'的init() '功能。您可以簡單地使用TypeScript構造函數。 (除非你已經從你的構造函數中調用'init()') –

+1

@BenBeck更好的是,從Angular 1.5.x開始,你可以使用'$ onInit(){}'withTypeScript,然後你甚至沒有從你的構造函數中調用它。當模塊實例化時它會自動啓動。 – Lex

回答

0

你不要在你的選擇有一個ng-model所以我只是假設你想將對象存儲從acctLocationCollection。要做到這一點,你只需要構建你ng-options如下:

<select class="selAccounts" 
     name="DeliverToCustomerNumber" 
     id="DeliverToCustomerNumber" 
     ng-model="alc.selectedAcctLocation" 
     ng-options="ac as ac.aliasName for ac in alc.acctLocationCollection"> 
</select> 

angular.module('app', []) 
 
    .controller('ctrl', function() { 
 
    this.selectedAcctLocation = {}; 
 
    this.acctLocation = [{ 
 
     aliasName: "1234", 
 
     deliverToCustomerNumber: "25235" 
 
    }, { 
 
     aliasName: "2345", 
 
     deliverToCustomerNumber: "23523" 
 
    }]; 
 
    console.log(this.acctLocation); 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script> 
 
<div ng-app="app" ng-controller="ctrl as alc"> 
 
    By object: <select ng-model="alc.selectedAcctLocation" ng-options="ac as ac.aliasName for ac in alc.acctLocation"></select><br/> 
 
    By (key,value): <select ng-model="alc.selectedAcctLocation" ng-options="value as value.aliasName for (key,value) in alc.acctLocation"></select> 
 
    <div style="margin-top:10px;"> 
 
    alc.acctLocation: 
 
    <ul> 
 
     <li ng-repeat="(key,value) in alc.acctLocation">{{key}}: {{value}}</li> 
 
    </ul> 
 
    </div> 
 
    <div>selectedAcctLocation: {{alc.selectedAcctLocation | json}}</div> 
 
</div>

+0

我現在就試試這個。我需要ng模型嗎? – user2320476

+0

這就是你如何存儲用戶選擇的值,但是 - 沒有它Angular不會呈現你的選項。 – Lex

+0

謝謝。我試過上面的代碼,下拉菜單中沒有顯示任何內容。它仍然是空的。 – user2320476