繁体   English   中英

具有子对象的Ng选项

[英]Ng-options with subobject

这是我的数组:

$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/

我究竟做错了什么?

您需要像这样更改选择:-

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

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

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

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

它可能会帮助你!

您的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];
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM