Nei*_*eiL 3 javascript angularjs angular-ui-router
我在angularjs.我试图在url中添加参数$location.search('sid', key);.key的值来自另一个服务器的http请求.这是append参数的代码.
.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;
});
}]);
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是.随着sid被添加到url中.该console.log放我test1Controller越来越调用两次.Sid用于连接套接字连接.
是否有任何解决方法在url追加后调用控制器.
您可以使用 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: '/'
});
}])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1478 次 |
| 最近记录: |