使用ng-options时,没有使用ng-model获取完整的对象详细信息

时间:2015-12-14 10:08:34

标签: angularjs cordova

我有一个属性cardDetails,它有一个对象数组

var cardDetails = [{
            "cardHolderName":"XXXXXXXXXXXXXXXXXXXXXXXXXX",
            "cardNumber" :'1234'
            "mobileNumber":"91098765678",
            "cardType":"C",
            "cardStatus":0,
            "created":1449572395000,
            "updated":1449572395000
            }]

在我的html中,我使用ng-options填充

<select ng-change="" ng-model="payment.card" ng-options="card.cardType as (card.cardType==='C' ? 'Credit Card':'Debit Card') +' - XXXX XXXX XXXX '+card.cardNumber for card in cardList">

我正在我的控制器中检索选定的下拉列表,如下所示

paymentCard = $scope.payment.card;

我得到的值只是C,它是卡片类型,但我想要所选对象的所有值。但如果我使用下面的ng-options,我会得到整个对象

<select ng-change="" ng-model="payment.card" ng-options="card.cardType for card in cardList">

我的目标是向用户显示自定义的相关信息,同时我想在后端获取其他字段。任何想法我错过了什么?

1 个答案:

答案 0 :(得分:1)

更新你的html,这是完整的例子:

在ng-options中,当你设置&#34; card.cardType&#34;时,你可以获得对象中的全部或一个参数。你会得到&#34; cardType&#34;但如果你设置&#34; card&#34;你会得到你所有的对象。

&#13;
&#13;
<!DOCTYPE html>
<html ng-app="app" ng-controller="ExampleController">

<head>
  <title>stackoverflow</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>

  <select ng-model="payment.card" ng-options="card as card.cardNumber for card in cardDetails"></select>
  {{payment.card}}

  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0-rc.0/angular.js"></script>

  <script>
    angular.module("app", [])
      .controller("ExampleController", ["$scope",
        function($scope) {
          $scope.cardDetails = [{
            "cardHolderName": "XXXXXXXXXXXXXXXXXXXXXXXXXX",
            "cardNumber": "1234",
            "mobileNumber": "91098765678",
            "cardType": "C",
            "cardStatus": 0,
            "created": 1449572395000,
            "updated": 1449572395000
          }];
        }
      ])
  </script>
</body>

</html>
&#13;
&#13;
&#13;