Kicco
Kicco

Reputation: 33

AngularJS - link model to search string without watching

it might be a dumb question but... is it possible to link an input to the $location.search() without using a $watch? i mean

that would be the html extract

<input type="text" ng-model="vm.search.text"/>

this the controller (using controllerAs in the router)

this.search = $location.search()

and i would like to have the edits reflected in the search string without using a $watch, so something like https://url.blah.blah?text=

This because i hate using watches and i wanna keep my controllers as slim as humanly possible (and the application is pretty big, AND i kinda like this idea)

i know i might just build a directive and shut up, but it sounds too convoluted to me.

Please let me know if it just doesn't make sense :)

Thanks!

Upvotes: 0

Views: 122

Answers (1)

Josep
Josep

Reputation: 13071

You could try this, in your controller do this:

.controller('testCtr', function($location){
   this.search = {
       text:'',
       fn: $location.search.bind($location, 'text');
   };
})

And then in your view do this:

<div ng-controller="testCtr as vm">
   <input type="text" ng-model="vm.search.text" ng-change="vm.search.fn(vm.search.text)"/>
</div>

Upvotes: 1

Related Questions