Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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

share|improve this question
    
Looks like you have a typo on this one "ng-repate" the correct is "ng-repeat" – diegochavez Dec 23 '15 at 12:58
    
yes sry i have rectified that one but still the same result – Mohan Gopi Dec 23 '15 at 13:06
    
see what you get here in html {{ products | json }} – vijay Dec 23 '15 at 13:15
up vote 1 down vote accepted

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

share|improve this answer
    
thanks you bakkal – Mohan Gopi Dec 23 '15 at 13:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.