2017-03-31 96 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