0

** angular newbie alert **

I have a web page that is called with several querystring values.

I would like to populate a couple of data binding areas with values coming in from the querystring.

<span class="username">{{firstName}}{{lastName}}</span>

and i am parsing the querystring using this

  var vars = [], hash;
  var q = document.URL.split('?')[1];
  if (q != undefined) {
    q = q.split('&');
    for (var i = 0; i < q.length; i++) {
      hash = q[i].split('=');
      vars.push(hash[1]);
      vars[hash[0]] = hash[1];
    }
  }

  alert(vars['lastName']);

I am not sure how to actually drive the values into the data binding fields.

3
  • 1
    there's nothing C# here Commented Jun 6, 2016 at 19:07
  • there are plenty of Angular tutorials online have you tried looking at some and trying to understand how AngularJS works..? Commented Jun 6, 2016 at 19:10
  • Yes, the tuts are great... but in the question, i have not found a solution that i have outlined above. I placed a textbox at the bottom of the page, and updated the textbox with the value from the querystring, nothing was updated in the bound-data field - until i manually enter a value into the textbox by hand, and then it updates the {{lastName}} so what i need to do is replace the part of putting the value into the textbox, the changing the text for it to display. Commented Jun 6, 2016 at 20:01

2 Answers 2

1

Use $location provider from angular.

You can do var searchObject = $location.search();

Then get key of this object. Like searchObject.lastName

angular.controller("ctrl",['$location',function($location){ /**Ctrl code **/

}]);

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

5 Comments

I tried this direction the first time, but i get an error that $location is undefined. both inside the jquery(document).ready code block and outside.
are you using angular ? you must inject $location inside you controller
yes - i added this code to verify it is working <span class="username">{{firstName}}{{lastName}}{{1 + 2}}</span> and 3 is displayed
please explain how DI would be implemented in the question
you would do angular.controller("ctrl",['$location',function($location){}]); Check answer
0

I had to go with several portions from several tuts along with a hammer to get something into place.

That being said, placement of the code is particularly important.

a majority of the code has to be done at the beginning of the page, and in the head section.

the suggestion for the $location was a good one, but it seemed to fail each time i placed it into the code block.

Here is the primary code block...

  <script>

var dashboardApp = angular.module('dashboardApp', []);
dashboardApp.controller('userNameCtrl', function ($scope) {

  var vars = [], hash;
  var q = document.URL.split('?')[1];
  if (q != undefined) {
    q = q.split('&');
    for (var i = 0; i < q.length; i++) {
      hash = q[i].split('=');
      vars.push(hash[1]);
      vars[hash[0]] = hash[1];
    }
  }

  $scope.firstName = vars['firstName'];
  $scope.lastName = vars['lastName'];
});

<body ng-app="dashboardApp">

<span class="username" ng-controller="userNameCtrl">{{firstName}} {{lastName}}</span>

This code block fails with no error messages, but it crashes way out, and blows out the remaining javascript block

    var dashboardApp = angular.module('dashboardApp', []);

dashboardApp.controller('userNameCtrl', function ($scope, $location) {

  alert("First Name is - " + $location.search()['firstName']);

  var vars = [], hash;
  var q = document.URL.split('?')[1];
  if (q != undefined) {
    q = q.split('&');
    for (var i = 0; i < q.length; i++) {
      hash = q[i].split('=');
      vars.push(hash[1]);
      vars[hash[0]] = hash[1];
    }
  }

  $scope.firstName = vars['firstName'];
  $scope.lastName = vars['lastName'];
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.