Reputation: 7724
i have no problem in pushing the data in array here is my code.when i use console.log(products) outside the function i am getting an empty array but when i use the console.log(products) inside the controller i am getting my data.
angular.module('ob3App.lead')
.controller('LeadProductCtrl',['$scope','$http', function($scope,$http) {
$scope.namegetfunction = function() {
var products=[];
$http.get("http://5.9.42.45:8080/easycloud1/org.openbravo.service.json.jsonrest/Product?l=saiy5k&p=saiy5k")
.success(function(response) {
console.log(response);
$scope.names = response.response.data;
console.log($scope.names.length);
$scope.names.forEach(function(item) {
console.log(item.name);
products.push(item.name);
})
console.log(products);
})
.error(function(responses){
alert('error');
});
};
$scope.namegetfunction();
}]);
while i am trying to use ng-repeat i am not able to view it in list i dont know what i am doing wrong.
<ion-view>
<ion-header-bar class="bar bar-header bar-positive flat">
<button class="button button-positive" ng-click="back()"><i class="ion-arrow-left-c"> </i></button>
<h1 class="title">Products</h1>
</ion-header-bar>
<ion-content>
<ul class="list">
<li class="item" ng-repeat="i in products" ui-sref="leadProduct" >
<div>{{i}}</div>
</li>
</ul>
</ion-content>
</ion-view>
the above code is to view my names in a list formate but i am struggling where i have done wrong
Upvotes: 2
Views: 7300
Reputation: 55448
You don't have a $scope.products
which is where AngularJS looks for it when you use ng-repeat="i in products"
, what you have is:
var products = [];
...
products.push(...)
Which isn't enough, you have to assign it to the scope, you have to do
$scope.products = products
When you are done collecting the data, or initialize it in the scope directly from the beginning e.g.
$scope.products = [];
$scope.names.forEach(function(item) {
$scope.products.push(item.name);
})
You were checking with console.log(products)
instead of $scope.products
too, which might have thrown you off course thinking that everything was okay :)
Upvotes: 1