我正在尝试调用从数据库获取数据的外部脚本,因此可以填充选择下拉列表。到目前为止,我已经尝试过了,但是却加载了空的li元素。这应该是什么都没有显示的正确计数?

我的控制器

app.controller('agenciesController', function($scope, $http) {
  var url = "/ajax/getAgencies.php";

  $http.get(url).success( function(response) {
    $scope.agencies = response;
    $scope.agenciesArray = [];
    angular.forEach(response, function(value, key){
      $scope.agenciesArray.push(value);
    })
    console.log($scope.agenciesArray);
  });
})


我的HTML

<body ng-controller="Controller">
   <div ng-controller="agenciesController">
      <ul ng-repeat="agencyName in agencies">
        <li>{{agenciesArray.agencyID}}</li>
      </ul>
   </div>
</body>


更新-
此代码有效,但不仅返回一个响应,而且返回全部。

  <div ng-controller="agenciesController">
    <ul ng-repeat="agencyName in agenciesArray">
      <li>{{agencyName}}</li>
    </ul>
  </div>


javascript - AngularJS,从外部源获取数据并插入数组-LMLPHP

最佳答案

agencyArray是一个数组。 agencyArray.agencyID将是未定义的。这就是为什么你要空李。

您打算这样做吗?

<ul ng-repeat="agency in agenciesArray">
        <li>{{agency.agencyName}}</li>
      </ul>

07-24 09:44
查看更多