Answer the question
In order to leave comments, you need to log in
Creating a module in angular?
There is a task to make sure that the parameters for the filter in ng-repeat are in the url (as with a GET request). And with feedback. The filter has changed, the url has changed, and vice versa. I achieved this in the following way. There is a search object that is passed to the filter and initialized as follows:
var searchObj = $location.search();
$scope.search = {
name : searchObj.name || "",
group : searchObj.group || "0",
type : searchObj.type || "employee",
};
$scope.$watch('search', function() {
$scope.updateUrl();
},true);
$scope.updateUrl = function(){
$scope.search.name ? $location.search("name",$scope.search.name ) : $location.search("name", null);
$scope.search.group !== "0" ? $location.search("group",$scope.search.group) : $location.search("group",null);
$scope.search.type ? $location.search("type",$scope.search.type) : $location.search("type",null);
}
$rootScope.$on('$locationChangeSuccess', function () {
$scope.updateSearch();
});
$scope.updateSearch = function(){
var searchObj = $location.search()
if (searchObj.type) {$scope.search.type = searchObj.type};
if (searchObj.name) {$scope.search.name = searchObj.name};
if (searchObj.group) {$scope.search.group = searchObj.group};
}
Answer the question
In order to leave comments, you need to log in
It would be best to make a service that contains the current search value, has a setter and getter, as well as a subscription to locationChange, and when changing the value (both through the setter and when changing the url), a broadcast is called. We already listen to this broadcast in the controller and do what we want. That is, the scheme is as follows: controller / location -> {search value change} -> service -> {search value changed} -> everyone who is subscribed.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question