每次在Ionic中导航到页面时都运行部分控制器代码?

时间:2015-11-23 15:10:25

标签: angularjs ionic

在我的控制器中,数据点似乎只在第一次打开此页面时从服务加载数据。下次没有。至少我的结论是这样的。

每次加载页面时,从服务中获取数据的好方法是什么?

(function(){
  angular.module('appname')
    .controller('EnterValueCtrl', ['$scope', '$state', '$filter', '$translate', 'SharedPropertiesSrvc',
      function($scope, $state, $filter, $translate, SharedPropertiesSrvc) {

        $scope.datapoint = {
          value: SharedPropertiesSrvc.datapoint.value,
          inputType: SharedPropertiesSrvc.datapoint.inputType
        };

      }]);
})();

这是我的服务代码:

(function(){
  angular.module('appname')
    .factory('SharedPropertiesSrvc', [
      function(){
        return {
          datapoint: {
            inputType: '',
            value: ''
          }
      };
      }])
})();

1 个答案:

答案 0 :(得分:2)

我在这里找到了一个很好的答案:

  

http://forum.ionicframework.com/t/how-to-excecute-the-controller-every-time/14893/3

我用过

$scope.$on('$ionicView.beforeEnter', function() {
    //runs every time the page activates
});