繁体   English   中英

如何使用$ http.get从服务器查看数据

[英]How to view the data from server using $http.get

我有一个名为$scope.mobileBrandArray= [];的空数组$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(); $http.get()函数$scope.getMobileBrandArray(); 它工作正常,但我需要在$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函数。

或将ng-click='getMobileBrandArray()'到要启动get函数的按钮上。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM