In this blogpost I want to show a simple AngularJS NgResource Demo with ASP.NET WebAPI querying data as JSON.
The application follows the “normal” way of seperating the data access also on client side into services.
Before you can use angulars ng resource you have to include it into your application like this:
So here you are providing “ngResource” to your app.
After this you can inject the ngResource service provided by angular into your services:
The first argument you pass in is the url with the optional id (:id). The ng resource service now can call get create and delete methods ist a rest convenient way.
ngresource is providing you the following methods:
Querying all resources:
Pay attention to the empty object we are passing in. You could add a single id here if you only want to query one single resource.
In case you want to add a new resource, a person in this case, you can just call the “save” method and pass in the person you want to save:
The service maps the “save”-method to the POST-Action from REST and is executing a POST- call against the API.
Deleting is self explaining:
The ngResource provides a good way to interact with a rest api using very very small code but providing big functionality.