how to view the data from array in list using ionic and angularjs

只谈情不闲聊 提交于 2020-02-02 12:24:25

问题


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


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



来源:https://stackoverflow.com/questions/34435792/how-to-view-the-data-from-array-in-list-using-ionic-and-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!