由于 url 中的附加参数,控制器被调用两次
controller getting called twice due to append params in url
我在angularjs。我正在尝试使用 $location.search('sid', key);
在 url 中附加参数。 key 的值来自另一台服务器通过 http 请求。
这是追加参数的代码。
.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 用于连接套接字连接。
在 url 附加后调用控制器是否有任何解决方法。
确保你只写了一次控制器。在 ng-controller 或你的配置路由中写入:
app.config(['$routeProvider', function($routeProvider){
$routeProvider.when('/',
{
templateUrl: 'pages/home.html'
//Remove controller from here
});
}]);
home.html
<!-- Add the ng-controller in your view -->
<div ng-controller="MyItemsController">
<!-- Your stuff -->
</div>
尝试在您的配置中使用 reloadOnSearch
: false 它将阻止控制器加载。
您可以使用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: '/'
});
}])
我在angularjs。我正在尝试使用 $location.search('sid', key);
在 url 中附加参数。 key 的值来自另一台服务器通过 http 请求。
这是追加参数的代码。
.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 用于连接套接字连接。
在 url 附加后调用控制器是否有任何解决方法。
确保你只写了一次控制器。在 ng-controller 或你的配置路由中写入:
app.config(['$routeProvider', function($routeProvider){
$routeProvider.when('/',
{
templateUrl: 'pages/home.html'
//Remove controller from here
});
}]);
home.html
<!-- Add the ng-controller in your view -->
<div ng-controller="MyItemsController">
<!-- Your stuff -->
</div>
尝试在您的配置中使用 reloadOnSearch
: false 它将阻止控制器加载。
您可以使用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: '/'
});
}])