2015-07-21 22 views
0

我有此代碼段在我的控制器:範圍不被應用到變量的作用域

$scope.$on('$ionicView.enter', function() { 
$scope.nodeId = $stateParams.studynodeRef; 
    Studies.fetchCollection($scope.nodeId).then(function(data){ 
     $scope.y = angular.fromJson(data.list); 
     $scope.collections = angular.fromJson($scope.y[0].collections); 
     console.log($scope.collections); 
}) 
}); 

我之所以$範圍$上是因爲我想要的節點ID來更新我每次進入時間該視圖可以更新集合。現在在我有$ $範圍的功能,只是有Study.fetchCollection功能,我能夠通過集合在我的模板來遍歷這樣的:

<ion-list ng-controller="StudyCtrl"> 
     <ion-item ng-repeat="collection in collections" nav-clear menu-close ng-click="go('app.studies')" class="item item-icon-left brand-base-text-color"> 
      <i class="icon ion-ios-paper"></i> 
      {{collection.name}} 
     </ion-item> 

但現在不顯示任何內容。即使我嘗試顯示{{collections}},它也不會顯示任何內容。控制檯返回$ scope.collections爲對象的名單,但我不認爲正在應用的範圍$這就是爲什麼{{收藏}}是空白,我不知道如何使用$範圍。$適用()在這個背景。我真的很感激,如果有人能告訴我如何解決這個

+0

但是你有沒有試過使用$ scope。$ apply()已經? 。$ $範圍應用(功能(){$ = scope.collections angular.fromJson($ scope.y [0] .collections); }); – BastianW

+0

我想同樣的現在我得到一個$摘要已經在進行錯誤 – user1585869

回答

0

一個克服這個是創建一個功能,控制器內調用,並因爲控制器連接到視圖它每次都會得到更新的方式你進入視圖。像這樣的東西

$scope.onLoad = function() { 
    $scope.nodeId = $stateParams.studynodeRef; 
    Studies.fetchCollection($scope.nodeId).then(function(data){ 
     $scope.y = angular.fromJson(data.list); 
     $scope.collections = angular.fromJson($scope.y[0].collections); 
     console.log($scope.collections); 
    }) 
} 
$scope.onLoad(); 
+0

正確的,但看到的東西我想控制或良好的fetchCollection加載每次我訪問相同的看法時。我明白,視圖的控制器只會加載一次,除非刷新頁面。這就是爲什麼我使用$ scope。$ on('$ ionicView.enter',function() – user1585869