【问题标题】:location.path("#/") is not workinglocation.path("#/") 不工作
【发布时间】:2016-03-30 15:34:13
【问题描述】:

在我的代码中, location.path("#/") 不起作用我尝试了很多,但我不明白为什么它不起作用。有一个简单的逻辑,注册成功后弹窗关闭,通知栏插件会看到通知。

我从 4 小时就开始了。

注册

    (function () {
        'use strict'
        /*
         * Signup Controller
         * @param {$scope} Object
         * @return
         */
        function SignupCtrl($scope, $location, $window, $timeout, UserService, notifications) {
            //$scope.captchaError=false;
            //$scope.passwordPattern = '((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{8,})';
            $scope.doRegistration = function () {
                console.log("form validation status:" + $scope.frmSignup.$valid);
                if (true === $scope.frmSignup.$valid) {
                    console.log("validation successfully executed");
                    UserService
                            .register($scope.user)
                            .error(function () {
                                angular.element('#btnCancelSignup').triggerHandler('click');
                                notifications.showError({
                                    message: 'Ooops! there is error occured, please try again.',
                                    hideDelay: 1500, //miliseconds
                                    hide: true // boolean
                                });
                                $location.path("#/");
                            })
                            .then(function (res) {
                                if (res.data.status === 1) {
                                    console.log("success in registration");
                                    angular.element('#btnCancelSignup').trigger('click');
                                    notifications.showSuccess({
                                        message: 'Please check your email to complete registration.',
                                        hideDelay: 1500, //miliseconds
                                        hide: true // boolean
                                    });
                                    $location.path("#/");
                                    if(!$scope.$$phase) $scope.$apply();

                                }
                                Recaptcha.reload();
                            });
                }
            }
        }

        angular
                .module('AppWhizbite')
                .controller('SignupCtrl', ['$scope', '$location', '$window', '$timeout', 'UserService', 'notifications', SignupCtrl]);
    }());

【问题讨论】:

  • 尝试编辑哈希而不是路径。 $location.hash('/')
  • @LuisMasuelli:谢谢你的回答。但它不起作用。它在 url 中附加如下:localhost:1000/#/#%2F
  • 请注意,我使用了 '/' 而不是 '#/'。
  • 是的,我和你描述的一样。
  • 否则尝试 path('/') 而不是使用 #/

标签: angularjs mean


【解决方案1】:

尝试如下

$scope.$apply(function() { 
  $location.path('/');
});

为了更好地理解Angular $location.path not working,您可能会阅读另一篇文章

【讨论】:

  • 我阅读并尝试过,但它不起作用。我几乎应用了所有方式。
  • Ashraful:尝试相同的代码时出现错误。错误:[$rootScope:inprog] $digest 已经在进行中。我正在使用 stateprovider,我想重新加载整个页面。
  • 如果你使用的是 $stateProvider(ui-router 不是 ngRoute),你应该使用 $state.go('/') 替换 $location.path('/')。同样,您还没有注入和使用 ngRoute / ui-router 作为 app.module 和控制器中的依赖项。请说明您使用的是 ngRoute 还是 ui-router。
【解决方案2】:

来自$location doc

更改浏览器 URL 时不会导致整个页面重新加载。要在更改 URL 后重新加载页面,请使用较低级别的 API,$window.location.href。

所以,试试:

$window.location.href = '/';

【讨论】:

    猜你喜欢
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-01
    相关资源
    最近更新 更多