我有一个名为$scope.mobileBrandArray= [];的空数组

现在,我正在尝试使用函数内的$http.get()从服务器获取数据。

这是我的app.js代码

app.controller('myCtrl' , function($scope , $http){
    $scope.mobileBrandArray = [];
    $scope.getMobileBrandArray = function(){
      $http.get("http://192.168.101.4:9090/Mobile_ERP/rest/brand/")
        .success(function(response) {
          console.log(response);
         console.log(response[0].brandKey);
          $scope.mobileBrandArray=response;
          console.log($scope.mobileBrandArray);
        })
        .error(function(response) {
          console.log(response);
          alert("error ");
        })
    };
});


在这里,我无法在UI中查看我的数据,但是我尝试在没有$http.get()的情况下使用$scope.getMobileBrandArray();函数,它可以正常工作,但是我需要在$scope.getMobileBrandArray();之外的数组

这是我的HTML代码

<tr ng-repeat="mobileBrand in mobileBrandArray" ng-include="getTemplate(mobileBrand)">
    <script type="text/ng-template" id="display">
        <td>{{mobileBrand.brandCode}}</td>
        <td>{{mobileBrand.brandName}}</td>
        <td>{{mobileBrand.brandStatus}}</td>
        <td>{{mobileBrand.brandCreatedOn}}</td>
        <td>
            <button type="button" class="btn btn-primary" ng-click="editMobileData(mobileBrand)">Edit</button>
            <button type="button" class="btn btn-danger" ng-click="deleteMobileData(mobileBrand)">Delete</button>
        </td>
    </script>
    <script type="text/ng-template" id="edit">
        <td><input type="text" ng-model=mobileBrand.brandCode class="form-control input-sm"/></td>
        <td><input type="text" ng-model=mobileBrand.brandName class="form-control input-sm"/></td>
        <td><input type="text" ng-model=mobileBrand.brandStatus class="form-control input-sm"/></td>
        <td><input type="text" ng-model=mobileBrand.brandCreatedOn class="form-control input-sm"/></td>
        <td>
            <button type="button" class="btn btn-primary" ng-click="updateMobileData(mobileBrand)">Save</button>
            <button type="button" class="btn btn-danger" ng-click="reset()">Cancel</button>
        </td>
    </script>
</tr>

最佳答案

我看不到您在哪里调用getMobileBrandArray()函数。您有两种选择:

ng-init='getMobileBrandArray()'添加到表格顶部以自动运行$http.get函数。

或在要启动get功能的按钮上添加ng-click='getMobileBrandArray()'

10-04 21:49