How do you retrieve a single post on AngularJS?

Question!

I've been trying to pull a "single-post" request (ie. grabbing a single entry from my backend database) using AngularJS. But I was unable to understand the actual concept behind pulling data from a JSON file through a GET() request.

Eg: Within the controller.js / services.js, what should be done to pull all data from a single-post, such as "/portfolio/design/1", using either $xhr or angular.service()?

I've attempted to read through the $resource and also the tutorial, but I think I wasn't able to wrap my mind around how to actually go about doing it.

I sincerely hope someone can help provide a simple example to help me along my problem.

By : sgrobert


Answers
The simplest way is to use the $xhr object. For example:

// in controllers.js

function ItemController($xhr){
    var self = this;
    self.key = this.params.id;

    $xhr('GET', 'api/items/' + self.id, function(code, data) {
        self.item = data;
    });
}

// in services.js

angular.service('myApp', function($route, $location, $window) {
    $route.when('/item/:id', 
          {template: 'partials/showItem.html', controller: ItemController});
    // other logic for routing
}

// in partials/showItem.html

<h3>Now Viewing {{item.name}}</h3>
By : Jamie


This video can help you solving your question :)
By: admin