$ http.get()循环使用ng repeat

时间:2015-01-08 23:13:40

标签: angularjs angularjs-ng-repeat angularjs-http

我有2个实体,如下所示

person {
  personID,
  personName,
  Array of Cars (Person Car)
}

Cars {
  carID,
  carName
}

然后是Person-Car的关系实体,如下所示

PersonCar {
  personID,
  CarID,
  relatedProperty1,
  relatedProperty2
}

现在,为了在HTML页面上的数据上表示这一点,我正在做以下

    <div class="row" ng-repeat="person in persons">
       <div class="col-xs-6">
          {{person.personID}} - {{person.personName}}
       </div>
       <div class="col-xs-6">
          <div class="row">
             <div class="col-xs-12" ng-repeat="car in cars">
                {{person.carID}} - {{getCarName(person.carID)}} - 
              {{person.relatedProperty1}} - {{person.relatedProperty2}}
             </div>
             <!-- Each Car-->
          </div>
          <!-- Array of Cars-->
       </div>
       <!-- Array of Cars Section -->
    </div>
    <!-- Person Row-->                      

此外,正在检索数据如下

modelInfo.controller('modelInfoController', function ($scope, $http, $filter) {
    $http.get('http://localhost/person/123').
            success(function(data) {
              $scope.persons = data
            }

            $scope.getCarName = function(carID) {
            $http.get('http://localhost/car/54545').
                    success(function(data) {
                        $scope.persons = data.carName
            }                  
            }

            };

由于某些未知原因,脚本在getCarName函数上无限循环。请帮我理解这个问题。另外,如果这是一个很好的REST架构,请告诉我。

1 个答案:

答案 0 :(得分:0)

你错过了几个结束括号

modelInfo.controller('modelInfoController', function ($scope, $http, $filter) {
    $http.get('http://localhost/person/123').success(
        function(data) {
          $scope.persons = data
        }
    ); // <-- here, closing success function call

    $scope.getCarName = function(carID) {
        $http.get('http://localhost/car/54545').success(
            function(data) {
                $scope.persons = data.carName;
            }
        ); // <-- here too       
    };

}); // <-- and here, closing the controller call

对于REST,它取决于后端服务器端点的定义方式

虽然在客户端,您可以为您的资源定义服务,即使用REST方法的$ http包装器(获取,放置,删除,更新)