2

我正在使用 ui-router 和 ui-router extras

$scope.$on('$transitionStart', function (e, $transition$) {
    var params = {id:1};
    factory.webService(params)
        .success(function (data) {
            if (data.d == "Expired") {//Inspects the application session
                //Stops the state request and sents you to the login screen
                e.preventDefault();
                location.href = "login.html";
            }
            else if (data.d == "NotAllowed") {
                //Stops the state request and keeps you on the state you already are
                e.preventDefault();
            } else {
                //Proceed to load the requested state
            }
        })
        .error(function (data, status) {
            alert(data.Message);
        });
});

我需要在 $stateChangeStart 加载之前解决成功部分并且无法弄清楚如何去做。

有没有办法做到这一点?

编辑

所以我有这样的代码

.state('myState', {
        url: "/myState",
        templateUrl: 'views/template.html',
        controller: 'ctrlTemplate',
        viewId: 99,
        resolve: {
            fn: function ($stateParams, myFactory) {
                myFactory.checkSession({viewId:99})
                .then(function onSuccess(response) {
                    var data = response.data;
                    if (data.d == "Expired") {
                        throw "Expired";
                    }
                    else if (data.d == "NotAllowed") {
                        throw "NotAllowed";
                    } else {
                        //RETURN to chain data
                        return data;
                        //Proceed to load the requested state
                    }
                })
                .catch(function (response) {
                    //e.preventDefault();
                });
            }
        }
    })

$http.then函数仍在解析之后发生$stateChangeStart并且$transitionStart新状态已经加载。我可以在浏览器控制台调试器中看到它发生

请帮忙。

4

2 回答 2

1

是的,有更优雅的方法:通过 ui-router 解析

例子:

$stateProvider.state('users.profile', {
  url: '/:id',
  templateUrl: 'views/users.profile.html',
  controller: 'UsersController',
  resolve: {
    user: function($stateParams, UserService) {
      return UserService.find($stateParams.id);
    },
    tasks: function(TaskService, user) {
      return user.canHaveTasks() ?
        TaskService.find(user.id) : [];
    }
  }
});

请阅读以下有关高级路由和解析的文章以获取更多详细信息。

于 2017-02-02T23:38:40.133 回答
1

我需要等待 $http 响应并在.success函数中捕获它

.success方法(已弃用,现已从 AngularJS 1.6 中删除)无法拒绝承诺,但该.then方法能够成功转换为拒绝:

var promise = factory.webService(params)
    //.success(function (data) {
    .then( function onSuccess(response) {
        var data = response.data;
        if (data.d == "Expired") {
            //THROW to create a rejected promise
            throw "Expired";
            /*
            //Inspects the application session
            //Stops the state request and sents you to the login screen
            e.preventDefault();
            location.href = "login.html";
            */
        }
        else if (data.d == "NotAllowed") {
            //THROW to create a rejected promise
            throw "NotAllowed";
            /*
            //Stops the state request and keeps you on the state you already are
            e.preventDefault();
            */
        } else {
            //RETURN to chain data
            return data;
            //Proceed to load the requested state
        }
    })
    //.error(function onError(data, status) {
    .catch( function(response) {
        var data = response.data;
        var status = response.status;
        alert(data.Message);
        //THROW to chain rejection
        throw data.Message;
    });
});

通过使用returnorthrow语句,从httpPromise的.thenor方法解析的响应中创建一个新的派生 Promise。.catch

当 ui-router 状态的解析器函数获得拒绝的承诺时,状态更改被中止。


更新

所以我有这样的代码......

    //ERRONEOUS
    resolve: {
        fn: function ($stateParams, myFactory) {
            myFactory.checkSession({viewId:99})
            .then(function onSuccess(response) {
                var data = response.data;
                if (data.d == "Expired") {
                    throw "Expired";
                }

我不明白为什么 throw 不会触发 $stateChangeError。

解析器函数需要将承诺返回给路由器:

    resolve: {
        fn: function ($stateParams, myFactory) {
          //vvvvvv RETURN the promise
            return myFactory.checkSession({viewId:99})
            .then(function onSuccess(response) {
                var data = response.data;
                if (data.d == "Expired") {
                    throw "Expired";
                }

当一个函数省略一个return语句时,该函数返回一个值undefined。在这种情况下,路由器认为它是成功的,值为undefined

方法也是.catch错误的

    //ERRONEOUS   
          })
            .catch(function (response) {
                //e.preventDefault();
            });

.catch方法的拒绝处理程序省略return(or throw) 语句时,该函数返回 的值undefined。这会将拒绝转换为成功的承诺,并以undefined.

      //CORRECT   
            })
            .catch(function (response) {
                //THROW to chain rejection
                throw response;
                //e.preventDefault();
            });

函数式编程的经验法则是——总是返回一些东西

在承诺成功和拒绝的情况下:总是returnthrow某事。

于 2017-02-03T04:22:56.350 回答