简体   繁体   English

服务失败后如何解决状态

[英]How do i resolve state when service has been failed

Thanks for reading this question. 感谢您阅读此问题。 I am making AngularJS app. 我正在制作AngularJS应用。 I made a state and define resolve that is calling User.getProfile which is defined in User service. 我进入状态并定义了解析,该解析调用了User服务中定义的User.getProfile。 so this resolve is working when service has been succeeded to fetch profile but not working when it has been failed. 因此,此解决方法在服务成功获取配置文件时起作用,但在服务失败时不起作用。 let me post a code. 让我发布一个代码。

    .state('app.profile', {
        url: '/profile',
        views: {
            'menuContent': {
                templateUrl: 'views/pages/profile.html',
                controller: 'ProfileCtrl',
            }
        },
        resolve: {
            data: function(User) {
                return User.getProfile();
            }
        }
    })

and here is service i defined. 这是我定义的服务。

service.getProfile = function() {
        var deferred = $q.defer();

        HttpReq.sendRequest( RESTAPI.DEV_URL + RESTAPI.PROFILE,
            'GET',
            undefined,
            function(result) {
                /*

                */
                if (result.success) {
                    deferred.resolve(result);
                }
                else {
                    deferred.reject(result);

                }

            },
            function(error, response) {
                deferred.reject(error);

                //uiHelper.alert($scope, 'Login Error', response.message);
        });

        return deferred.promise;
    };

app.profile is resolved when defer.resolve is called in service. 在服务中调用defer.resolve时将解析app.profile。 but isn't resolved when defer.reject is called. 但是在调用defer.reject时无法解决。 Please correct me if i have problem in promise calling. 如果我在承诺电话方面遇到问题,请纠正我。

Thanks! 谢谢!

If the resolve function fail, you will probably want to display to the user something accordingly (like an error message). 如果resolve函数失败,您可能需要向用户显示相应的内容(例如错误消息)。 It would be better to let your resolve function return a promise (without then) and use the internal event $routeChangeError. 最好让您的resolve函数返回一个Promise(不包含),然后使用内部事件$ routeChangeError。

 myApp.run(['$rootScope',function($rootScope) {
  $rootScope.$on('$routeChangeError', function() {
    // what you want to do if an error occurs 
    // ex: $rootScope.displayErrorMessage = true
  });
}]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调整大小后如何裁剪 konva 图像? - How do I crop a konva image when it has been resized? 如何验证是否使用 Yup 验证设置了状态? - How do I validate if a state has been set using Yup validation? 当另一个按钮已获得焦点或具有内部焦点时,如何触发按钮的内部焦点状态? - How do you trigger a button's focus-within state when another button has been focused on or has a focused-within? 如何确定使用JavaScript SDK时twilio呼叫实际上已被应答? - How do I find out when a twilio call has actually been answered when using the JavaScript SDK? 完全加载并渲染图像后,如何调用方法? - How do I call a method when an image has been fully loaded and rendered? 在其他窗口中单击电子邮件验证链接后,如何返回到原始窗口 - How do i return back to the original window when a email verification link has been clicked in a different window 当数据已更改时,如何在 Realm Javascript 中获取旧的修改值 - How do i get old modification values in Realm Javascript when that data has been changed 首先填充另一个下拉框时,如何显示新的下拉框? - How do I make new drop down boxes appear when another one has been filled in first? 我如何知道使用原始WebSocket时是否已成功发送消息? - How do I know if an message has been sent successfully when using raw WebSocket? 如何检测何时使用 jQuery 按下了删除键? - How do I detect when the delete key has been pressed with jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM