realph
realph

Reputation: 4671

Angular JS: Change $location Params on ng-click

I'm currently using $location in my app so I can send parameters from the address bar to my $http GET request. Like so:

var params = $location.search();
$http.get(url, params).then(function(){
    ...
}

Is it possible to add an ng-click attribute onto an element in my view, so I can pass parameters into the address bar, so I can filter my GET request?

Something like so:

<div ng-click="genre=romance">Romance</div>
<div ng-click="genre=horror">Horror</div>

<div ng-click="year=2012">2012</div>
<div ng-click="year=2011">2011</div>

So when two buttons are clicked the queries chain themselves and url is transformed to: app.com/movies?genre=horror&year=2012, for example.

Is this possible with $location and am I going about this in the right way?

Any help is appreciated. Thanks in advance!

Upvotes: 1

Views: 303

Answers (1)

mat3e
mat3e

Reputation: 779

Search function is both getter and setter. You can e.g. put $location in $scope and then use it like this:

<div ng-click="$location.search('genre','romance')">Romance</div>
<div ng-click="$location.search('genre','horror')">Horror</div>
<div ng-click="$location.search('year','2012')">2012</div>
<div ng-click="$location.search('year','2011')">2011</div>

And modify $http.get:

$http.get($location.absUrl()).then(function(){...}

Upvotes: 1

Related Questions