javascriptangularjsrestangular-ui-routerngresource

How to GET Restful data for a particular id


I'm new to AngularJS and i would like to pass an id to my URI (from which i would make a rest call). To illustrate my problem better here's an example:

If a user clicks on a link with ui-sref="{{result.id}}" in my app.js I have something like -

$stateProvider.state("",{

url:"/{{result.id}}",
controller : "artleFeed",
templateUrl: "Feed/feed.html" 

Now I want to make a rest call with url something like -Mybackendproject/xyz/users/id

Now i want to pass my result.id to rest call url - id

I have read other people's post on how ngResource could do it and honestly didn't quite understand.

I would be really thankful if you could help me figure it out.

Thanks


Solution

  • You simply define your state like this:

    $stateProvider.state("result", {
      url: "/:id",
      controller : "articleFeed",
      templateUrl: "Feed/feed.html"
    });
    

    Then in the articleFeed controller, you reference $stateParams.id to access this variable:

    .controller('articleFeed', function ($scope, $state, $stateParams, ArticleService) {
      ArticleService.get({ id: $stateParams.id }, function (data) {
        $scope.article = data;
      });
    }