子对象的Ng选项

时间:2015-01-15 09:09:53

标签: javascript angularjs

这是我的阵列:

$scope.subscriptions = [
      {
        currency: {
          title: 'Euro',
          symbol: '€'
        },
        licenses: [
          {
            nrOfLicenses: 5,
            price: 500
          },
          {
            nrOfLicenses: 10,
            price: 750
          }
      ]
    },
    {
      currency: {
        title: 'SEK',
        symbol: 'kr'
      },
      licenses: [
        {
           nrOfLicenses: 5,
            price: 5000
         },
         {
            nrOfLicenses: 10,
            price: 7500
          }
      ]
    },    
  ]

  $scope.selectedSubscription = $scope.subscriptions[0];

我想要一个选择框,您可以选择货币。

我试过了:

<select ng-model="selectedSubscription.currency" ng-options="currency.title for currency in subscriptions"></select>

它无法正常工作。这是一个小提琴:http://jsfiddle.net/HB7LU/10066/

我做错了什么?

3 个答案:

答案 0 :(得分:5)

你需要像这样改变选择: -

<select ng-model="selectedSubscription.currency" ng-options="sub.currency.title for sub in subscriptions"></select>

这是更新的小提琴 http://jsfiddle.net/gbsLa4wh/

答案 1 :(得分:1)

我发现你的问题是关于你的选择器。您可以浏览所有订阅并从订阅项目中的对象货币中获取标题。但是你在订阅对象上迭代这个正确的代码

<div ng-controller="myCtrl">
    <select ng-model="selectedSubscription" ng-options="subscription.currency.title for subscription in subscriptions"></select>
    <pre>{{selectedSubscription | json}}</pre>
</div>

答案 2 :(得分:0)

它可能对你有所帮助!!

您的HTML代码示例

<div ng-controller="myCtrl">
    <select ng-model="selectedSubscription.currency" ng-options="CuSub.currency.title for CuSub in subscriptions"></select>
    <pre>{{selectedSubscription.currency | json}}</pre>
</div>

您的AngularJs代码示例

var myApp = angular.module('myApp',[]);

angular.module('myApp')
.controller('myCtrl', function($scope){
    $scope.subscriptions = [
      {
        currency: {
          title: 'Euro',
          symbol: '€'
        },
        licenses: [
          {
            nrOfLicenses: 5,
            price: 500
          },
          {
            nrOfLicenses: 10,
            price: 750
          }
      ]
    },
    {
      currency: {
        title: 'SEK',
        symbol: 'kr'
      },
      licenses: [
        {
           nrOfLicenses: 5,
            price: 5000
         },
         {
            nrOfLicenses: 10,
            price: 7500
          }
      ]
    },    
  ]        
  $scope.selectedSubscription = $scope.subscriptions[0];
});