href调用后控制器未被重置

时间:2015-06-14 04:40:50

标签: angularjs ionic-framework

我在Ionic Framework中有一个AngularJs控制器。

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

3 个答案:

答案 0 :(得分:3)

没关系,我找到了答案。 结果证明我的控制器默认是缓存的。

我使用此代码修改了状态提供程序,现在使用新模型刷新视图。

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

这里的区别是cache:false

干杯!

答案 1 :(得分:1)

默认情况下,离子视图为cached,但您可以在视图中手动将cache设置为false,这将使控制器再次加载。

read more here,你所做的一切也是正确的,但我个人更喜欢我在这里提到的方法,因为它会给予更多的控制

答案 2 :(得分:1)

如果你想以任何理由保持你的页面缓存,你可以包装你需要在另一个函数内运行的所有函数,然后在事件$ ionicView.beforeEnter或afterEnter或enter,你可以调用该函数。然后,您可以保持页面缓存,并且每次输入页面时仍然运行所有功能。例如,在我制作的应用程序中,我不希望主页未被删除,但我需要一些功能来拉动 每次输入页面时都会有新数据。所以我这样做了:

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

这样页面可以保持缓存,但我的应用程序仍然表现得像我想要的那样。再看一些ionicView方法:http://ionicframework.com/docs/api/directive/ionView/