Solved

Get URL in angular.js

Posted on 2015-02-11
8
55 Views
Last Modified: 2015-02-15
Hello, I thought this would be simple and straight forward but it seems not :)

Example URL - /index.htm?myVab=Test

Now on my page "index.htm" I would like to simply bring the variable "myVab" in to my controller.js file

I've read a lot about location etc but surely there must be a simple way of retrieving this?
0
Comment
Question by:DanteAdmin
  • 5
  • 3
8 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 40602682
So, basically this is not the "Angular way" of doing this.

To do it in a fully supported way you  should use the Router: https://docs.angularjs.org/api/ngRoute/service/$route
This means that you need to start using routes instead of querystrings.

Anyway, if you want to keep using querystrings you can use
$location.search()

Open in new window

This returns a JSON object which has the key/value pairs that match your querystring.
0
 

Author Comment

by:DanteAdmin
ID: 40605082
Do I need to declare $location somehow in the controller before I can use it ?

I'm just getting that the variable isn't declared

Will also read up on $ruote

Thanks
0
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
ID: 40605178
The $location has to be injected in the controller as any other resource:
angular.module('myapp')
  .controller('MyController', ['$scope', '$location', function($scope, $location){
    // your controller code here
  }]);

Open in new window

0
 

Author Closing Comment

by:DanteAdmin
ID: 40609468
Many thanks - works a charm
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:DanteAdmin
ID: 40609501
I can get it to update the URL etc but having problems grabbing existing values and presenting them

If I type in the browser:
index.htm?myVab=test

and on "index.htm" I have

{{$scope.myVab}}


In Controller.js
$location.search(myVab)

myVab = $location.search(myVab);



Is the way I'm trying to do this not possible? If I then change in the URL I'm hoping it will change in the controller file and present ?
0
 

Author Comment

by:DanteAdmin
ID: 40609603
Update:

This is the code that from reading a lot should work, but it doesn't

I'm able to manipulate the string from the same controller file i.e. I can add params to the string fine, but I can't read it and present it


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

or to ge the whole string this should work ?

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

But still doesn't return a thing
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 40610512
Hi mate,
What you really need is to use routes.
They give you a lot of advantages over query string parameters.

One of the obvious is that they don't trigger a full page refresh if you change them, and as you're using AngularJS, you should be embracing the single-page design, otherwise it makes little sense.

I'm writing from my phone but once I get my hands on a computer I can bake you a simple demo.
Meanwhile you can Google for angular routing, you'll find plenty of examples.

I'll be back... :-)
0
 

Author Comment

by:DanteAdmin
ID: 40610565
Thanks Alexandre

I've managed to get it working but then I realised all changes on the page triggers the same function so URL just updates even when I want it to actively leave the page...

So I'm starting to realise I need to spend the time to get my head around routes

This is that post - http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28617032.html#a40610000

Again many thanks for taking the time
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now