Mohan Gopi Mohan Gopi - 1 month ago 11
AngularJS Question

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

Answer

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.