如何使用离子和angularjs查看列表中从阵列中的数据 [英] how to view the data from array in list using ionic and angularjs

查看:106
本文介绍了如何使用离子和angularjs查看列表中从阵列中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在推阵列中的数据在这里没有问题是我的code.当我使用的console.log(产品)我得到一个空数组但功能外,当我使用的console.log(产品)在控制器内我收到我的数据。

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();

 }]);

当我尝试使用NG-重复我不能够在列表我不知道我做错了查看。

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>

以上code是在列表中甲查看我的名字,但我挣扎在那里我有做错了

the above code is to view my names in a list formate but i am struggling where i have done wrong

推荐答案

您没有 $ scope.products 这就是AngularJS寻找它,当你使用 NG-重复=我的产品,你所拥有的是:

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);
})

<子>你用的console.log(产品)检查而不是 $ scope.products 太大,这可能抛出你偏离轨道以为一切都好:)

You were checking with console.log(products) instead of $scope.products too, which might have thrown you off course thinking that everything was okay :)

这篇关于如何使用离子和angularjs查看列表中从阵列中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆