2

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

3
  • Looks like you have a typo on this one "ng-repate" the correct is "ng-repeat" Commented Dec 23, 2015 at 12:58
  • yes sry i have rectified that one but still the same result Commented Dec 23, 2015 at 13:06
  • see what you get here in html {{ products | json }} Commented Dec 23, 2015 at 13:15

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.