如何使用ionic和angularjs查看列表中数组的数据

时间:2015-12-23 12:31:51

标签: angularjs ionic-framework

我在推送数组中的数据没有问题这里是我的代码。当我在函数外使用console.log(产品)我得到一个空数组但是当我在控制器内部使用console.log(产品)时我正在获取我的数据。

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-repeat时我无法在列表中查看它我不知道我做错了什么。

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

上面的代码是在列表格式中查看我的名字,但我在做错的地方挣扎

1 个答案:

答案 0 :(得分:1)

当你使用$scope.products时,你没有ng-repeat="i in products"这是AngularJS寻找它的地方,你拥有的是:

var products = [];
...
products.push(...)

哪个还不够,你必须将它分配到范围,你必须这样做

$scope.products = products

完成收集数据后,或直接从开头将其初始化,例如

$scope.products = [];
$scope.names.forEach(function(item) {
      $scope.products.push(item.name);
})

您正在使用console.log(products)而不是$scope.products进行检查,这可能会让您误以为一切正常:)