4

Im trying to grab the values from parameters in the url and use that to populate some textboxes on my page but i cant figure it out. can anyone help (p.s im new to angular)

This is my example texboxes

<div class="container">
    val1: <input type="text" ng-model="vm.txt1" />
    val2:<input type="text" ng-model="vm.txt2" />
</div>

here is the example code in my controller

(function() {
    'use strict';

     angular.module('app').controller('exampleController', ExampleController);

     ExampleController.$inject = ['$scope', '$location', '$window', '$http', '$templateCache'];       

     function ExampleController($scope, $location, $window, $http,   $templateCache) {
         var location = $location;
         var url = $scope.location.url('http://www.example.com/examplepage?name=todd&age=20');
         $scope.vm.txt1 = $scope.location.search().name;
         $scope.vm.txt2 = $scope.location.search().age;
     };
})();

2 Answers 2

4

You have:

   var location = $location;
   var url = $scope.location.url('http://www.example.com/examplepage?name=todd&age=20');
    $scope.vm.txt1 = $scope.location.search().name;
    $scope.vm.txt2 = $scope.location.search().age;

Should be:

  $scope.vm.txt1 = $scope.location.search().name;
  $scope.vm.txt2 = $scope.location.search().age;

You can remove the local "var location = $location" as $location will suffice. There is no need to add $location to your $scope for this instance.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much this did it for me.
2

Controller

In the controller, just set:

$scope.urlParams = $location.search();

HTML

<input type="text" ng-model="urlParams.name" />
<input type="text" ng-model="urlParams.age" />

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.