I am trying to apply default params to a URL if they don't exist. Successively, I copy URL params to a local variable ($scope.params
) and then load content:
// If limit and sort params are defined
if ($location.search().hasOwnProperty('limit') && $location.search().hasOwnProperty('sort')) {
// Update scope params
$scope.params = $location.search();
// Load content
$scope.loadContent();
} else {
// Merge URL params and scope params; don't override URL params
$location.search(utilityFactory.mergeObjects($location.search(), $scope.params, false)); // REMEMBER THIS AS #1
// Replace browser history entry
$location.replace();
}
I have another function that I invoke when changes to $scope.params
are made to update URL params:
$scope.updateParams = function () {
// Reset pagination
delete $scope.params.skip;
// Update URL params
$location.search($scope.params); // REMEMBER THIS AS #2
};
In #1, the controller gets initialized and the control goes into if(){}
instead of else{}
. Perfect. However, in #2, the $location.search()
does not refresh the controller. If I do a force reload after #2 using $route.reload()
, the controller gets initialized twice.
What am I doing wrong?