【问题标题】:Ionic Framework no refresh list when navigateIonic Framework 导航时没有刷新列表
【发布时间】:2015-05-18 16:05:45
【问题描述】:

我正在 AngularJS 中创建一个移动应用程序。我调用一个调用 API 的资源来给我值。一切正常,但连接速度慢或 3G $ 范围对我来说并不酷,因此在浏览项目列表时是旧的。

SERVICES.JS

 .factory('Exercises', function($resource) {

    // localhost: Local
    // 79.148.230.240: server
    return $resource('http://79.148.230.240:3000/wodapp/users/:idUser/exercises/:idExercise', {
        idUser: '55357c898aa778b657adafb4',
        idExercise: '@_id'
    }, {
        update: {
            method: 'PUT'
        }
    });
});

控制器

.controller('ExerciseController', function($q, $scope, $state, Exercises) {

         // reload exercises every time  when we enter in the controller
         Exercises.query(function(data) {
             $scope.exercises = data;
         });

         // refresh the list of exercises
         $scope.doRefresh = function() {

             // reload exercises
             Exercises.query().$promise.then(function(data) {
                 $scope.exercises = data;
             }, function(error) {
                 console.log('error');
             });

             // control refresh element 
             $scope.$broadcast('scroll.refreshComplete');
             $scope.$apply();
         }

         // create a new execersie template
         $scope.newExercise = function() {
             $state.go('newExercise');
         };

         // delete a exercise
         $scope.deleteExercise = function(i) {

             // we access to the element using index param
             var exerciseDelete = $scope.exercises[i];

             // delete exercise calling Rest API and later remove to the scope
             exerciseDelete.$delete(function() {
                 $scope.exercises.splice(i, 1);
             });
         };
     })

APP.js

angular.module('wodapp', ['ionic', 'ngResource', 'wodapp.controllers','wodapp.services'])

// Run
.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // ionic is loaded
  });
})

// Config
.config(function($stateProvider, $urlRouterProvider, $ionicConfigProvider) {
  $stateProvider
    .state('slide', {
      url: '/',
      templateUrl: 'templates/slides.html',
      controller: 'SlideController'
    })
    .state('login', {
      url: '/login',
      templateUrl: 'templates/login.html',
      controller: 'LoginController'
    })
    .state('dashboard', {
      url: '/dashboard',
      templateUrl: 'templates/dashboard.html',
      controller: 'DashboardController'
    })
    .state('exercise', {
      url: '/exercise',
      templateUrl: 'templates/exercises.html',
      controller: 'ExerciseController'
    })
    .state('newExercise',{
      url: '/newExercise',
      templateUrl: 'templates/newExercise.html',
      controller: 'NewExerciseController'
    });
  $urlRouterProvider.otherwise('/');
});

【问题讨论】:

  • 你的错误对我来说不是很清楚:但是连接速度慢或 3G $ 范围并不酷,因此在浏览项目列表时是旧的。你工厂的查询功能在哪里 :Exercises.query ??
  • 使用angular资源时会自动调用查询函数

标签: angularjs ionic-framework scope frameworks 3g


【解决方案1】:

如果您想重新加载控制器逻辑的一部分,则每次激活视图时:

.controller('ExerciseController', function(
  $q, 
  $scope, 
  $state, 
  Exercises,
  $ionicView
) {
   // reload exercises every time  when we enter in the controller
   $ionicView.enter(function(){
     // This gets executed regardless of ionicCache
     Exercises.query(function(data) {
       $scope.exercises = data;
     });;
   });
});

否则,您可以在.state() 上使用reload 选项

【讨论】:

    猜你喜欢
    • 2019-12-15
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 2015-08-21
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多