0

Here is what I am trying to do:

Angularjs

$http.get("/api/comments/"+id)
              .success(function(data){
                   $scope.comments = data;       
              })

And I have in laravel /api/comments/id route...

How can I take params with angularjs for example if user visit this url; /home/1 I need to take 1 and send to angular get request?

4
  • Are you using $stateProvider or $routeProvider? Commented Aug 6, 2015 at 13:12
  • How is your routes defined in your application? Commented Aug 6, 2015 at 13:16
  • Vladimir, do you have route configuration in your angular javascript code? Commented Aug 6, 2015 at 13:20
  • No... I need help with it... (: Commented Aug 6, 2015 at 13:25

3 Answers 3

0

If you are using $stateProvider like

$stateProvider
    .state('comments', {
        url: "/api/comments/:id",
        templateUrl: 'comments.html'
    })

You can take out the id using $stateParams.id

Or if you are using $routeProvider like;

$routeProvider 
    .when("/api/comments/:id"{
        templateUrl: 'comments.html'
    })

You can take out the id using $routeParams.id

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

3 Comments

I am new to angularjs could you explain me in more details how to define routes?
I don't need to take id from api I need to take id from /home/id and than send get request to /api/comments/id
Any how you have to configure routes in your application to get it done
0

If you're not using router a dirty way is to get it from $location service and regexp integer from $location.path().

Comments

0

Simplest solution is to inject $routeParams in your controller. And then set

app.controller(controllerName, function ($scope, $http, $routeParams) {
    $scope.id = $routeParams.id; 
}

You will then get $scope.id = 1 when visiting api/comments/1

6 Comments

I need when user visit /home/1 to take 1 and submit to get req with that api.
this will work always. Updated the answer. Please see
Which is the controller you are accessing while going to the /home/1 page?
Can you log $routeparams also?
It just return undefined... Here is my whole code: stackoverflow.com/questions/31858415/…
|

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.