2016-02-04 46 views
0

我從一個JSON文件拿到項目,並將它們發送到我的$scope如何管理從JSON文件返回的對象列表?

//JS controller 

.controller('MyController', function($scope, $log) { 
    $http.get('JSON_FILE_URL').success(function(response){ 
     $scope.responseData = response; 
     $scope.items = rundom(response); 
    }); 

    $scope.refresh = function(data) { 
     $log.log('refresh data :'+data); 
     $scope.items = rundom(data);  
    }; 
} 

// view.html 

<ion-view view-title="myTitle" ng-controller="MyController"> 
    <button ng-click="refresh({{responseData}})">refresh</button> 
    <ion-content overflow-scroll="true" padding="true" ng-cloak class="fullPage"> 
     <div class="item item-text-wrap" ng-repeat="item in items"> 
      <h1>{{ item['title'] }}</h1> 
     </div> 
     //some code here 
    </ion-content> 
</ion-view> 

我有檢查的HTML元素的項目是有我的函數中。

<button ng-click="refresh({"id": 0, "title": "title 0"},{"id": 1, "title": "title 1"})">refresh</button> 

當我點擊按鈕刷新,空話happends但 我得到未定義的日誌:refresh data :undefined

它是一個類型的問題至極,我沒有考慮到?

+1

你嘗試NG點擊=「刷新(responseData)」 ? – Oliver

+0

@Oliver:你是對的:) – Drwhite

回答

0

您不需要在ng-click中使用尖括號。

ng-click="refresh()" 

它可以無論如何訪問$範圍裏面的功能,所以沒有必要把它傳遞

+0

不能工作,因爲它不需要任何參數。 – Drwhite

0

的Olivier的答案是正確的:ng-click="refresh(responseData)"