在angularjs中调用另一个数组中的json数组

时间:2014-07-22 10:30:39

标签: javascript arrays json angularjs

我是angular and json的新手,所以如果有人可以帮助我的话。

我有json数组我有用户和用户的卖家名字买东西。

Json:

[
  {
    "name":"Kakaro",
    "sallers":[
     {"sellername":"Naruto","payment":"250","date":"07\/07\/2014","receipt":"popup"},
     {"sellername":"Sasuka","payment":"502","date":"08\/07\/2014","receipt":"popup"}
    ]
  },
  {
    "name":"Collin Ferall",
    "sallers":[
      {"sellername":"Jonny deep","payment":"250","date":"07\/07\/2014","receipt":"popup"},
      {"sellername":"Brad Pitt","payment":"502","date":"08\/07\/2014","receipt":"popup"}
    ]
  }
]

我想做的事情:当我们选择用户时,用户的卖家数据就会出现在桌面上。

我见过许多解决方案,但我在所有解决方案中都失败了。现在我开始在我开始的地方。

问题:我不知道如何在另一个数组角度中正确调用数组。

HTML:

<div id="main" class="row" ng-controller='payment'>
  <div>
    <label>Client Name:</label><br/>
    <select class="form-control input-sm" name="client_name">
      <option ng-repeat="names in name" value="{{names.name}}" ng-model="select">{{names.name}}</option>
    </select>
  </div>
  <div id="seller" class="margins_top">
    <table class="table table-bordered">
      <thead>
        <tr>
          <td>Seller Name</td>
          <td>Payment</td>
          <td>Date</td>
          <td>Payment Receipt</td>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="sellers in seller">
          <td>{{sellers.sellername}}</td>
          <td>{{sellers.payment}}</td>
          <td>{{sellers.date}}</td>
          <td>{{sellers.receipt}}</td>
        </tr>
      </tbody>
    </table>
  </div><!-- /#seller -->
</div>  

控制器js:

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


app.controller('payment',function($scope,$http){
  getrecord(); // Load all available tasks 
  function getrecord(){  
    $http.post("angular/ajax/payment.php").success(function(data){
      $scope.name = data;
      $scope.seller = data[0].sallers;
    });
  }
});

我创造了小提琴,但它没有用,但我仍在添加它。

Fiddle

1 个答案:

答案 0 :(得分:1)

尝试这样

HTML中的

    <select class="form-control input-sm" name="client_name" ng-model="select">
  <option ng-repeat="names in name" value="{{names.name}}">  {{names.name}}</option>
</select>

并在你的JS中

$scope.$watch('select', function(newvalue) {
            angular.forEach($scope.name, function(value, key) {
                if(value.name == newvalue) {
                $scope.seller = value.sallers;
                }
            });
            });