由于在url中追加params,控制器被调用两次 [英] controller getting called twice due to append params in url
本文介绍了由于在url中追加params,控制器被调用两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在angularjs。我试图使用 $ location.search('sid',key);
在URL中追加参数。 key的值来自另一个服务器的http请求。
这是append参数的代码。
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中。 console.log
我输入 test1Controller
两次调用。 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.
在网址附加后是否有任何解决方法可以调用控制器。
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中追加params,控制器被调用两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文