gpt4 book ai didi

javascript - 如何使用 Resolve 重定向到页面 - AngularJS

转载 作者:行者123 更新时间:2023-12-03 10:20:29 25 4
gpt4 key购买 nike

我在编写正确的解析函数时遇到问题。

我的目标是在用户首次打开应用程序时将其重定向到 View ,以设置一些信息(例如访问代码)。流程需要如下:

  1. 当用户打开应用时,他首先进入“auth”状态
  2. 该状态需要解析函数localSetupResolve
  3. 如果被拒绝,statechange 错误会将用户重定向到状态“setup”

我尝试了以下操作,但不断收到以下错误:

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'
})

})

安装工厂

.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;
})

最佳答案

你有一个无限的摘要循环。

我不确定你是否可以将你的服务注入(inject)到 RUN 部分。

我将执行以下操作

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

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

这样,您只需注入(inject)一次服务。

关于javascript - 如何使用 Resolve 重定向到页面 - AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29659876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com