I am very new to angularJS. I am searching for accessing services from RESTful API, but I didn't get any idea. How can I do that?
相关问题
- Design RESTful service with multiple ids
- Axios OPTIONS instead of POST Request. Express Res
- Plain (non-HTML) error pages in REST api
- angularJS: ui-router equivalent to $location.searc
- Separate AngularJS Controllers Into Separate Files
相关文章
- Passing variable through URL with angular js
- Watch entire object (deep watch) with AngularJS
- Angular ng-if change span text
- Can ng-show directive be used with a delay
- AngularJS $routeParams vs $stateParams
- Got ActiveRecord::AssociationTypeMismatch on model
- Multiple parameters in AngularJS $resource GET
- How to set class/style of accordion heading in Ang
Just to expand on
$http
(shortcut methods) here: http://docs.angularjs.org/api/ng.$http//Snippet from the page
//available shortcut methods
Welcome to the wonderful world of Angular !!
There are two (very big) hurdles to writing your first Angular scripts, if you're currently using 'GET' services.
First, your services must implement the "Access-Control-Allow-Origin" property, otherwise the services will work a treat when called from, say, a web browser, but fail miserably when called from Angular.
So, you'll need to add a few lines to your web.config file:
Next, you need to add a little bit of code to your HTML file, to force Angular to call 'GET' web services:
Once you have these fixes in place, actually calling a RESTful API is really straightforward.
You can find a really clear walkthrough of these steps on this webpage:
Using Angular, with JSON data
Good luck !
Mike
For instance your json looks like this : {"id":1,"content":"Hello, World!"}
You can access this thru angularjs like so:
Then on your html you would do it like this:
This calls the CDN for angularjs in case you don't want to download them.
Hope this helps.
The $http service can be used for general purpose AJAX. If you have a proper RESTful API, you should take a look at ngResource.
You might also take a look at Restangular, which is a third party library to handle REST APIs easy.
Option 1: $http service
AngularJS provides the
$http
service that does exactly what you want: Sending AJAX requests to web services and receiving data from them, using JSON (which is perfectly for talking to REST services).To give an example (taken from the AngularJS documentation and slightly adapted):
Option 2: $resource service
Please note that there is also another service in AngularJS, the
$resource
service which provides access to REST services in a more high-level fashion (example again taken from AngularJS documentation):Option 3: Restangular
Moreover, there are also third-party solutions, such as Restangular. See its documentation on how to use it. Basically, it's way more declarative and abstracts more of the details away from you.