Solved

Get URL in angular.js

Posted on 2015-02-11
8
60 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:DanteAdmin
ID: 40609468
Many thanks - works a charm
0
 

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

688 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