Solved

Get URL in angular.js

Posted on 2015-02-11
8
57 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jQuery when a text link is clicked? 16 37
jQuery dialog box help 2 17
Change to file doesn't show up 16 51
How to create a table with buttons 3 21
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

809 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