使用angularjs

时间:2017-03-31 14:22:49

标签: angularjs

我有一个带有init方法的打字稿控制器。我能够在messageresult中获取我想要的所有数据 这很好。

         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"

1 个答案:

答案 0 :(得分: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>