0

I have the following URL:

http://myUrl.com/#/chooseStyle?imgUpload=6_1405794123.jpg

I want to read the imgUpload value in the query string - I'm trying:

alert($location.search().imgUpload);

But nothing alerts, not even a blank alert - but console reads:

$location is not defined

I need this value to add into a controller to pull back data, and also to carry into the view itself as part of a ng-src

Is there anything I'm doing wrong? this is my app config:

capApp.config(function($locationProvider, $routeProvider) {
  $locationProvider.html5Mode(false);
  $routeProvider

  // route for the home page
  .when('/', {
    templateUrl : '/views/home.html',
    controller  : 'mainController'
  })

  // route for the caption it page
  .when('/capIt', {
    templateUrl : '/views/capIt.html',
    controller  : 'mainController'
  });
}):

This is the view:

<div class="container text-center">
  <h1 class="whiteTextShadow text-center top70">Choose your photo</h1>
</div>

<script>
  alert($location.search().imgUpload);
</script>

Main controller:

capApp.controller('mainController', function($scope) {
  $scope.message = 'Whoop it works!';
});

My end goal is that I can find a solution to capturing and re-using data from the query string.

I will also mention, this is only my first week in Angular, loving it so far! A lot to learn...

4
  • We can't find a bug without seeing the code. Commented Mar 8, 2015 at 14:53
  • ok, that's really it though, the view simply alerts (to begin with anyway) what else can I show you to allow you to help? Thx for the reply Commented Mar 8, 2015 at 14:56
  • If the view triggers the alert, and the alert isn't shown, then either there is an exception that should show in the console, or the view actually does NOT trigger the alert. Since we don't know what the console displays, and since you won't show the code, we can't help. The code matters. With 2000+ rep points, you should know that. Commented Mar 8, 2015 at 14:58
  • Yeah you're right, apologies - amended question JB Commented Mar 8, 2015 at 15:08

2 Answers 2

1
<script>
  alert($location.search().imgUpload);
</script>

You're making two mistakes here:

  1. executing code while the page is loading, and the angular application is thus not started yet
  2. assuming $location is a global variable. It's not. It's an angular service that must be injected into your controller (or any other angular component). This should cause an exception to be thrown and displayed in your console. Leave your console open always, and don't ignore exception being thrown.
Sign up to request clarification or add additional context in comments.

1 Comment

I've actually just thought about this and your comments are great, many thanks - just getting my head around MVC and it's clicking slower than I want it to! Thanks again
1
You should not do this
<script>
  alert($location.search().imgUpload);
</script>

// you need to inject the module $location 
//(either in service, or controller or wherever you want to use it)
// if you want to use their APIs

capApp.controller('mainController', function($scope, $location) {
  $scope.message = 'Whoop it works!';
  //use API of $location
  alert($location.search().imgUpload);
});

2 Comments

Many thanks for your answer - I gave correct answer to JB as the explanation was priceless to me - but I really appreciate your help
My pleasure @DarrenSweeney. If it helped u, I ll be more satisfied. :) and the correct answer is very important for new visiters as they first look at that particular answer and beleives it to be totally correct. Even I felt, his answer was more helpful for similar situation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.