2015-06-14 24 views
0

我在Ionic Framework中有一個AngularJs控制器。href調用後控制器沒有被重置

.controller('LocationDetailCtrl', ['$scope','$cordovaGeolocation','$cordovaCamera', '$cordovaFile','$stateParams','Location', LocationDetailCtrl]); 
    function LocationDetailCtrl ($scope, $cordovaGeolocation, $cordovaCamera, $cordovaFile, $stateParams, Location) { 
     $scope.locationRow = {}; 
     $scope.test = ""; 

     $scope.images = []; 

     Location.getById($stateParams.locationId).then(function(result){ 
      //alert("I'm in"); 
      $scope.locationRow = result; 
     });    
    } 

我在查看代碼的地方,它是:

<ion-item class="item-remove-animate item-icon-right" ng-repeat="location in locations" type="item-text-wrap" href="#/locations/{{location.id}}/all"> 
    <h2>{{ location.aplicant_name}}</h2> 
    <p>{{ location.form_type }}</p> 
    <i class="icon ion-chevron-right icon-accessory"></i> 
    <ion-option-button class="button-assertive" ng-click="remove(location)" translate>Delete</ion-option-button> 
</ion-item> 

在我stateprovider我有這樣的:

.state('location-detail', { 
     url: '/locations/{locationId}', 
     abstract: true,  
     templateUrl: 'templates/location-detail.html', 
     controller: 'LocationDetailCtrl'  
}) 
.state('location-detail.all', { 
    url: '/all', 
    views: { 
     'loc-detail-view': { 
      templateUrl: 'templates/location/location-map-all.html' 
     } 
    } 
}) 

我的問題是,第一HREF點擊我獲取數據庫的值,它一切正常。但是當我回去再按另一個列表時間時,我會得到與之前相同的價值。

原來Location.getById()未被第二次調用。

回答

2

從不介意,我找到了答案。 原來我的控制器默認被緩存了。

我用這段代碼修改了狀態提供程序,它現在用新模型刷新視圖。

.state('location-detail', { 
     url: '/locations/{locationId}', 
     cache: false, 
     abstract: true, 
     templateUrl: 'templates/location-detail.html', 
     controller: 'LocationDetailCtrl' 
    }) 

這裏的區別是cache:false

乾杯!

+0

資料來源:http://stackoverflow.com/a/29363932/456912 –

1

默認情況下離子視圖爲cached但是,您可以在視圖中手動將cache設置爲false,這將使控制器再次加載。

read more here,你所做的是如何過也是正確的,但我個人更喜歡我在這裏提到的如果你想讓你的頁面緩存以任何理由,你可以包裝所有它會給更多的控制

+0

緩存一些視圖而不緩存其餘的似乎是合乎邏輯的。 我不想全局或全部禁用緩存轉發視圖。 感謝您指向詳細的文檔頁面。 –

1

方法你需要在另一個函數中運行,然後在$ ionicView.beforeEnter或afterEnter或enter事件中運行,你可以調用該函數。然後,您可以保持頁面緩存,並且每次輸入頁面時仍然可以運行所有功能。例如,在我創建的應用程序中,我不想讓主頁取消緩存,但每次頁面輸入時,我都需要一些功能來提取新數據 。所以我這樣做:

$scope.$on('$ionicView.beforeEnter', function() { 
      $scope.doRefresh(); 
     }); 

這樣的方式頁面可以保持緩存,但我的應用程序仍然像我希望它的行爲。看看更多的ionicView方法:http://ionicframework.com/docs/api/directive/ionView/