0

In my app I'm reading an URL parameter which I'd like to pass to my Angular controller, which will then load a JSON file.

For example index.html?id=1234 should make make the controller load data/1234.json

My current code results in an injector error:

var id = getUrlParam('id')

app.controller('myController', function ($scope, $http, id) {
  $http.get("data/"+id+".json").then(function(res) {
      $scope.posts = res.data
  })
});

What am I doing wrong?

1
  • @idlebery - Please share how you add $routeParams.id because I am getting erro. Commented Nov 14, 2014 at 21:21

2 Answers 2

2

$routeParams is what you need. Basicaly get your parameter like $routeParams.id

Additional information - https://docs.angularjs.org/api/ngRoute/service/$routeParams

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

Comments

-1

Try this :

var getParameterByName = (function(a) {
    if (a == "") return {};
    var b = {};
    for (var i = 0; i < a.length; ++i)
    {
        var p=a[i].split('=', 2);
        if (p.length == 1)
            b[p[0]] = "";
        else
            b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
    }
    return b;
})(window.location.search.substr(1).split('&'))
var id = getParameterByName["id"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.