-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
I've tried but couldn't re-create the problem in Plunker. But I've done some step-debugging, and got some information. Hopefully people who are familiar with UI-router's codebase can give me an answer.
I'm building a simple search app. The search result page is defined as:
.state('search', {
url: '/search?q',
templateUrl: 'views/search.html',
controller: 'SearchCtrl'
})
And in SearchCtrl, I have two scope functions to add/remove search terms:
$scope.searchByTerm = function(term) {
termsService.addTerm($scope.selectedTerms, term);
$state.go($state.current, {q: $scope.selectedTerms}, {reload: true});
};
$scope.removeTerm = function(term) {
termsService.removeTerm($scope.selectedTerms, term);
$state.go($state.current, {q: $scope.selectedTerms}, {reload: true});
};
The termsService.addTerm function simply pushes term into $scope.selectedTerms array, and termsService.removeTerm removes term from the array if it exists. Since I want the page to reload and fetch data according to new search terms, I have set reload to be true.
With this, the SearchCtrl gets initiated twice (the state transition happens twice), when $scope.selectedTerms has 2 or more items. I've tried all possible ways of rewriting this, and it still consistently runs twice.
I've stepped into source code of both angular-ui-router.js
and angular.js
, and found this listener has triggered the transitionTo
for the second time:
$urlRouterProvider.when(state.url, ['$match', '$stateParams', function ($match, $stateParams) {
if ($state.$current.navigable != state || !equalForKeys($match, $stateParams)) {
$state.transitionTo(state, $match, { location: false });
}
}]);
This is in the registerState
function. From the console, $match
and $stateParams
are equal. I'm not sure why this step is executed in this case.
(angular-ui-router#0.2.11, angular#1.2.26)