0

I'm trying to one-way bind a URL Parameter 'email' to the value of an input field using Angular. The desired effect is that whenever the email parameter in the URL changes, the input field will change to match the new email parameter vaue. I'm doing this in my js file:

$scope.inputEmail = $location.search()['email']

To modify the value of the input field I'm using its ng-model, 'inputEmail'. When I do this, the value of the input field is not changing when I change the URL email parameter. What am I doing wrong?

3 Answers 3

1

Not sure if this may help bur from the Angular documentation https://docs.angularjs.org/api/ng/service/$location

// given URL http://example.com/#/some/path?foo=bar&baz=xoxo
var searchObject = $location.search();
// => {foo: 'bar', baz: 'xoxo'}

Then, for your example:

$scope.auxSearch = $location.search();
$scope.inputEmail = $scope.auxSearch.email;

And probably it could be just, not sure if this will work anyway:

$scope.inputEmail  = $location.search().email;
Sign up to request clarification or add additional context in comments.

Comments

0

You can try this:

$scope.$on('$routeUpdate', function(){
    $scope.inputEmail = $location.search()['email']
});

Note that you HAVE to set reloadOnSearch to false on the route for $routeUpdate to be available

Otherwise you could use $rootscope:

$rootScope.$on('$locationChangeSuccess', function(event){
      $scope.inputEmail = $location.search()['email']
})

It will update the model value, when you change your query params

Comments

0

I found my problem, I forgot to include $location and it's dependencies where I defined my component controller. Thanks for the help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.