Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you retrieve a single post on AngularJS?

I am trying to pull a single-post request (i.e. grabbing a single entry from my back-end database) using Angular. But I was unable to understand the actual concept behind pulling data from a JSON file through a GET() request.

Example :

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.

like image 920
sgrobert Avatar asked Jan 19 '26 23:01

sgrobert


1 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>
like image 163
Jamie Avatar answered Jan 22 '26 13:01

Jamie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!