我如何使用angular $ scope.whichItem = $ routeParams.itemId读取我的嵌套json数据

时间:2016-10-27 13:33:00

标签: angularjs json

大家好,所以我有一个控制器,我正在使用一些路径ID,所以我可以将我的列表指向另一个页面。 我的控制器看起来像这样:

RecordsControllers.controller('DetailsController', ['$scope', '$http','$routeParams', function($scope, $http, $routeParams) {
  $http.get('js/data.json').success(function(data) {
    $scope.records = data;
    $scope.whichItem = $routeParams.itemId;

       if ($routeParams.itemId > 0) {
      $scope.prevItem = Number($routeParams.itemId)-1;
    } else {
      $scope.prevItem = $scope.records.length-1;
    }

    if ($routeParams.itemId < $scope.records.length-1) {
      $scope.nextItem = Number($routeParams.itemId)+1;
    } else {
      $scope.nextItem = 0;
    }
  });
}]);

我的列表使用id路由发送到我的详细信息页面:

<li class="chat cf animation" ng-repeat="item in records | filter: query | orderBy: chatsOrder:direction | limitTo:limit">
                <a href="#details/{{records.indexOf(item)}}" >
<!--      <img ng-src="images/{{item.shortname}}_tn.jpg" alt="Photo of {{item.name}}">-->
      <div class="info row" ng-repeat="error in item.errors">

          <div class="col s12">
           <div class="row">
           <div class="col s2 push10"><p class="type_message">Error Type&#58;</p></div>

              <div class="col s10 pull2">
               <p >{{error.type}}</p>

              </div>
          </div>
          </div>





              <div class="col s12">

          <div class="row">


          <div class="col s2 push10">
              <p class="type_message"> Error Message&#58;</p>
              </div>

              <div class="col s10 pull2"><p>{{error.message}}</hp></div>




                  </div>
          </div>




<!--        <h3>{{item.gender}}</h3>-->
      </div>
                    </a>
            </li>

然后我用这段代码在另一边阅读

<section class="chatinfo">
  <div class="chat cf" ng-model="records">
    <a href="#/details/{{prevItem}}" class="btn btn-left">&lt;</a>
    <a href="#/details/{{nextItem}}" class="btn btn-right">&gt;</a>
    <h1>{{records[whichItem].errors.type}}</h1>
    <div class="info">
      <h3></h3>
      <img ng-src="images/{{records[whichItem].shortname}}_tn.jpg" alt="Photo of {{records[whichItem].name}}">
      <div class="bio"></div>
    </div>
  </div>
  <a href="index.html">&laquo; Back to search</a>
</section>

我的问题,我不工作,我不知道为什么,这就是我的json数据

[{
    "errors": [{
        "type": "ActionView::Template::Error",
        "message": "undefined method `average_price' for nil:NilClass",
        "backtrace": [{
            "file": "[PROJECT_ROOT]/app/views/quotes/show.html.haml",
            "line": 1,
            "function": "_app_views_quotes_show_html_haml___1123891298168566182_70087807002600"
        }]
    }]}]

我的问题在这里

<section class="chatinfo">
  <div class="chat cf" ng-model="records">
    <a href="#/details/{{prevItem}}" class="btn btn-left">&lt;</a>
    <a href="#/details/{{nextItem}}" class="btn btn-right">&gt;</a>
    <h1>{{records[whichItem].errors.type}}</h1>
    <div class="info">
      <h3></h3>

      <div class="bio"></div>
    </div>
  </div>
  <a href="index.html">&laquo; Back to search</a>
</section>

我无法读取数据

1 个答案:

答案 0 :(得分:0)

你好我们找到了我能做到的事情

 <h1 ng-repeat="item in records[whichItem].errors">

      {{item.type}}

      </h1>