Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS-静默更改$location-删除查询字符串_Angularjs - Fatal编程技术网

AngularJS-静默更改$location-删除查询字符串

AngularJS-静默更改$location-删除查询字符串,angularjs,Angularjs,是否有任何方法可以使用angular在url栏中静默更改路由 用户单击指向以下地址的电子邮件链接: /verificationExecuted?verificationCode=xxxxxx 加载页面时,我希望读取验证代码,然后清除它: if($location.path() == '/verificationExecuted'){ this.registrationCode = this.$location.search()["verificationCode"]; t

是否有任何方法可以使用angular在url栏中静默更改路由

用户单击指向以下地址的电子邮件链接:

/verificationExecuted?verificationCode=xxxxxx
加载页面时,我希望读取验证代码,然后清除它:

 if($location.path() == '/verificationExecuted'){
     this.registrationCode = this.$location.search()["verificationCode"];
     this.$location.search("verificationCode", null); //Uncomment but make this silent!

     if(registrationCode != null) {
         ....
     }
     else $location.path("/404");      
 }
当我清除它是路由的剩余部分(“/verificationExecuted”)时会发生什么,但是路由会重新触发,因此它再次出现,没有verificationCode,并直接转到404


我想在不做任何其他事情的情况下删除代码。

我的一个项目也有类似的要求

在这种情况下,我所做的就是利用服务

app.factory('queryData', function () {
    var data;

    return {
        get: function () {
            return data;
        },
        set: function (newData) {
            data = newData
        }
    };
});
该服务随后在我的控制器中用作:

app.controller('TestCtrl', ['$scope', '$location', 'queryData',
    function ($scope, $location, queryData) {
        var queryParam = $location.search()['myParam'];
        if (queryParam) {
            //Store it
            queryData.set(queryParam);
            //Reload same page without query argument
            $location.path('/same/path/without/argument');
        } else {
            //Use the service
            queryParam = queryData.get();
            if (queryParam) {
                //Reset it so that the next cycle works correctly
                queryData.set();
            }
            else {
                //404 - nobody seems to have the query
                $location.path('/404');
            }
        }
    }
]);
您可以始终将路线上的选项设置为
false。

如果只有查询字符串发生更改,它将阻止路由重新加载:

$routeProvider.when("/path/to/my/route",{
   controller: 'MyController',
   templateUrl: '/path/to/template.html',
   //Secret Sauce
   reloadOnSearch: false
});
试试这个

$location.url($location.path())

有关$location的更多详细信息,请参见。我通过添加一个更改路径并取消事件的方法解决了此问题

public updateSearch(){
var un=this.$rootScope.$on(“$routeChangeStart',(e)=>{
e、 预防默认值();
un();
});
此.location.search('new',search.searchFilter);
如果(!在历史记录中保留上一个路径)此.location.replace();
}

这将重新加载路径您是救生员!