2015-04-15 31 views
0

我在編寫正確的解析函數時遇到問題。如何使用Resolve重定向到頁面 - AngularJS

我的目標是在第一次打開應用程序以設置一些信息(如訪問代碼)時將用戶重定向到視圖。流程需要如下:

  1. 當用戶打開應用程序時,他首先進入狀態,「權威性」
  2. 這種狀態需要解析功能localSetupResolve
  3. 如果拒絕,statechange誤差將用戶重定向到狀態 '設置'

我嘗試以下,但我不斷收到以下錯誤:

https://docs.angularjs.org/error/ $ rootScope/infdig P0 = 10 & P1 =%5B%5D

Error: $rootScope:infdig Infinite $digest Loop

基本上,當消化循環被切斷,將其轉發到設置我的應用程序保持解析功能,然後。我想阻止這個摘要循環,就像authResolve可以正常工作一樣(下面)。

app.js

//IONIC STANDARD LINES OF CODE 

.run(function ($rootScope, $state, $log, $ionicHistory, SetupFactory) { 

    // 
    // 
    $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) { 
    // 
    switch (error) { 
     case "RESOLVE_SETUP_ERROR": 
     // 
     console.log("going to setup") 
     $state.go('setup'); 
     break 
     default: 
     // 
     $state.go('auth'); 
     break 
    } 
    console.log("$stateChangeError: error: ", error) 
    }); 

}) 


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

    // 
    // 
    $urlRouterProvider.otherwise('/auth'); 

// DOES NOT WORK 
    var localSetupResolve = function($q, SetupFactory) { 

    var qLocal = $q.defer(); 

    var FOPEN = SetupFactory.getFOPEN(); 
    console.log("LOCALSETUPRESOLVE", FOPEN) 
    switch (FOPEN) { 
     case true: 
     qLocal.resolve(true); 
     break 
     case false: 
     qLocal.reject("RESOLVE_SETUP_ERROR") 
     break 
    } 



    return qLocal.promise; 
    } 


    // 
    // WORKS FINE 
    var authResolve = function ($q, Auth) { 
    var qResolve = $q.defer(); 
    var AuthObj = Auth.getAuthObj(); 
    console.log("authResolve", AuthObj) 
    switch (AuthObj.authStatus) { 
     case true: 
     qResolve.resolve("AUTH_RESOLVE_SUCCESS"); 
     break 
     case false: 
     qResolve.reject("AUTH_RESOLVE_UNAUTHORIZED"); 
     break 
     default: 
     qResolve.reject("AUTH_RESOLVE_OTHER", AuthObj); 
     break 
    }; 
    return qResolve.promise; 
    }; 





// Ionic uses AngularUI Router which uses the concept of states 
    // Learn more here: https://github.com/angular-ui/ui-router 
    // Set up the various states which the app can be in. 
    // Each state's controller can be found in controllers.js 
    $stateProvider 

    // ----------------- 
    // Authentication & Setup 
    .state('auth', { 
    url: '/auth', 
    templateUrl: 'templates/single-auth.html', 
    controller: 'AuthCtrl', 
    resolve: { 
     localSetupResolve: localSetupResolve 
    } 
    }) 

    .state('setup', { 
    url: '/setup', 
    templateUrl: 'templates/single-setup.html', 
    controller: 'SetupCtrl' 
    }) 

}) 

SetupFactory

.factory('SetupFactory', function($localstorage) { 
    var self = this; 

    // 
    // Init 
    var tableName = "Setup"; 
    var tempTable = {}; 

    var FOPEN = $localstorage.getObject('FIRST_OPEN', '{}'); 
    if(FOPEN != false || FOPEN != true) { 
     FOPEN = false; 
    } 

    self.getFOPEN = function(){ 
     return FOPEN; 
    } 


    return self; 
}) 

回答

0

因此找出了問題所在。如here所述,您需要在進入另一個狀態之前阻止默認事件,以避免此摘要循環。

event.preventDefault(); 
1

你有一個無限循環消化。

我不確定您是否可以將服務注入RUN部分。

我會做以下

.run(function ($rootScope, $state, $log, $ionicHistory,$injector){ 
 

 
var $Auth = $Auth || $injector.get('SetupFactory');

這樣,你只注射一次爲您服務。

+0

想通了問題,請參閱我的回覆http://stackoverflow.com/a/29675066/4262057 – JohnAndrews

相關問題