简体   繁体   中英

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

I have an empty array called $scope.mobileBrandArray= [];

Now I am trying to get the data from my server using $http.get() inside the function.

Here is my app.js code

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 ");
        })
    }; 
});

Here I am not able to view my data in the UI, but I try $http.get() function without the $scope.getMobileBrandArray(); it works fine but i need my array outside the $scope.getMobileBrandArray();

Here is my HTML code

<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>

I don't see where you call the getMobileBrandArray() function. You have two options you could:

add ng-init='getMobileBrandArray()' to the top of your table to automatically run the $http.get function.

or add ng-click='getMobileBrandArray()' to a button you want to initiate the get function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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