2016-11-13 123 views
1

我有一個簡單的應用程序,顯示列表中的數據庫記錄。彈出後離子刷新列表

一切工作完美,除非我編輯項目或添加新項目時無法刷新列表。

我在彈出窗口中編輯和添加項目,這可能是導致問題的原因。你知道一種方法,我可以強制清單一旦我退出彈出窗口刷新?

我的代碼如下:

app.js

// Ionic Starter App 

// angular.module is a global place for creating, registering and retrieving Angular modules 
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html) 
// the 2nd parameter is an array of 'requires' 
// 'starter.controllers' is found in controllers.js 
angular.module('starter', ['ionic', 'ngResource', 'starter.controllers', 'starter.services']) 

.run(function($ionicPlatform) { 
    $ionicPlatform.ready(function() { 
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard 
    // for form inputs) 
     if (cordova.platformId === "ios" && window.cordova && window.cordova.plugins.Keyboard) { 
     cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 
     cordova.plugins.Keyboard.disableScroll(true); 

    } 
    if (window.StatusBar) { 
     // org.apache.cordova.statusbar required 
     StatusBar.styleDefault(); 
    } 
    }); 
}) 

.config(function ($stateProvider, $urlRouterProvider) { 

    // if none of the above states are matched, use this as the fallback 
    $urlRouterProvider.otherwise('/'); 

    $stateProvider 

    .state('home', { 
     url: '/', 
     views: { 
      'header': { 
       templateUrl: 'templates/header.html', 
       controller: 'headerCtrl' 
      }, 
      'content': { 
       templateUrl: 'templates/content.html', 
       controller: 'contentCtrl' 
      }, 
      'footer': { 
       templateUrl: 'templates/footer.html', 
       controller: 'footerCtrl' 
      } 
     } 
    }); 

}); 

controller.js

angular.module('starter.controllers', []) 

.controller('headerCtrl', function ($scope, $ionicListDelegate, $ionicPopup, Users) { 

    // Triggered by clicking the 'show delete buttons' button in header.html 
    $scope.toggleDelete = function() { 
     $ionicListDelegate.showDelete(!$ionicListDelegate.showDelete()); // toggle delete buttons in list 
    }; 

    // Triggered by clicking the 'add user' button in header.html 
    $scope.addUser = function() { 
     $scope.user = new Users(); 
     // An elaborate, custom popup 
     var myPopup = $ionicPopup.show({ 
      templateUrl: 'templates/userform.html', 
      title: 'Add new user', 
      subTitle: 'Firstname is mandatory', 
      scope: $scope, 
      buttons: [ 
       { text: 'Cancel' }, 
       { 
        text: '<b>Save</b>', 
        type: 'button-positive', 
        onTap: function (e) { 
         if (!$scope.user.firstname) { 
          // Prevent save if first name is empty 
          e.preventDefault(); 
         } else { 
          $scope.user.id = 0; 
          $scope.user.$save(); 
         } 
        } 
       } 
      ] 
     }); 
    }; 

}) 

.controller('contentCtrl', function ($scope, $ionicPopup, Users) { 

    // Populate list with all items from database 
    $scope.users = Users.query(); 

    // Triggered by clicking the 'delete user' button in content.html 
    $scope.onItemDelete = function (user) { 
     $scope.users.splice($scope.users.indexOf(user), 1); // remove the deleted item from the list 
     user.$delete(); // delete the record from the database 
    }; 

    // Triggered by clicking the 'edit user' button in content.html 
    $scope.editUser = function (user) { 
     $scope.user = Users.get({ id: user.id }); 
     // An elaborate, custom popup 
     var myPopup = $ionicPopup.show({ 
      templateUrl: 'templates/userform.html', 
      title: 'Edit user', 
      subTitle: 'Firstname is mandatory', 
      scope: $scope, 
      buttons: [ 
       { text: 'Cancel' }, 
       { 
        text: '<b>Save</b>', 
        type: 'button-positive', 
        onTap: function (e) { 
         if (!$scope.user.firstname) { 
          // Prevent save if first name is empty 
          e.preventDefault(); 
         } else { 
          $scope.user.$update(); 
         } 
        } 
       } 
      ] 
     }); 
    }; 

}); 

正如前面提到的,保存和更新工作正常 - 更新數據庫。

當彈出窗口關閉時,我無法獲取列表刷新。

我試過cache: false在app.js中的'內容'狀態,但沒有奏效。

任何想法?

+0

您在內容說,你的意思是你試過<離子視圖緩存視圖=「假」>? – Marko

+0

沒有,在這樣.STATE( '家',{ 網址app.js: '/', 觀點:{ '頭':{ templateUrl: '模板/ header.html中', 控制器:「headerCtrl ' }, '內容':{緩存:假, templateUrl: '模板/ content.html', 控制器: 'contentCtrl' }, '頁腳':{ templateUrl: '模板/ footer.html' , controller:'footerCtrl' } } }); – Damian

回答

2

我發現了一種方法,但如果有人有更好的方法,請張貼在這裏。

使用$state.reload();彈出關閉後重新加載列表

$ionicPopup.show({ 
    templateUrl: 'templates/userform.html', 
    title: 'Edit user', 
    subTitle: 'Firstname is mandatory', 
    scope: $scope, 
    buttons: [ 
     { text: 'Cancel' }, 
     { 
      text: '<b>Save</b>', 
      type: 'button-positive', 
      onTap: function (e) { 
       if (!$scope.user.firstname) { 
        // Prevent save if first name is empty 
        e.preventDefault(); 
       } else { 
        $scope.user.$update(); 
       } 
      } 
     } 
    ] 
}).then(function (res) { $state.reload(); }); 
};