由于在 url 中附加参数,控制器被调用两次 [英] controller getting called twice due to append params in url

查看:19
本文介绍了由于在 url 中附加参数,控制器被调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 angularjs.我正在尝试使用 $location.search('sid', key); 在 url 中附加参数.key 的值是通过 http 请求来自另一台服务器.这是附加参数的代码.

I am in angularjs. I am trying to append paramter in url using $location.search('sid', key);. The value of key is coming from another server by http request. Here is the code for append parameter.

.config(['$routeProvider',function ($routeProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'test1.html',
                controller: 'test1Controller'
            })
            .when('/message/:userId', {
                templateUrl: 'test2.html',
                controller: 'test2Controller'
            })
            .otherwise({
                redirectTo: '/'
            });
    }])
.run(['$route', '$rootScope', '$location' ,'getAuthDetails', function($route, $rootScope, $location,getAuthDetails) {
        var key;

        getAuthDetails.get().then(function(data){
            key = data.key;
            $rootScope.query = 'sid='+key;
            console.log($location.$$path);
            $location.search('sid', key);  //append parameter. This is calling controller twice
        });

        $rootScope.$on('$routeChangeSuccess', function () {
            $rootScope.query = 'sid='+key;
        });
    }]);

我遇到的问题是.由于 sid 被追加到 url 中.我放在 test1Controller 中的 console.log 被调用了两次.Sid 用于连接套接字连接.

The problem i am having is . As the sid get append in url. The console.log i put in test1Controller getting called twice. Sid is used to connect with socket connection.

是否有任何解决方法可以在 url append 之后调用控制器.

Is there any workaround to call the controller after the url append.

推荐答案

您可以使用 reloadOnSearch: false

示例:

.config(['$routeProvider',function ($routeProvider) {
        $routeProvider
            .when('/', {
                templateUrl: 'test1.html',
                controller: 'test1Controller',
                reloadOnSearch: false // wont reload the controller when the search query changes
            })
            .when('/message/:userId', {
                templateUrl: 'test2.html',
                controller: 'test2Controller'
            })
            .otherwise({
                redirectTo: '/'
            });
    }])

这篇关于由于在 url 中附加参数,控制器被调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆