简体   繁体   中英

How do I fetch data from a Angular JS application using a company's own API?

I'm working on the Front End Development for a company with a CRUD application using Angular JS. They want me to build out the views and grab the data using their API. (I've never done this)

He's telling me the syntax to grab the data would be something like this(this is just an example):

var getItems = function () {
        return $http
            .get('/api/1/items/available')
            .success(function (data, status, headers, config) {
                $scope.items = data.items;
            })
            .error(function (data, status, headers, config) {
                messageCenterService.add('danger', 'Unable to get the available Tickets and Passes.');
            });
    };

Then he said:

when you return the $http function, you can use it like:
getItems().then(... callback success, callback error...)
or move success() also
getItems().success(...).then(...)

Can someone help me understand what's going on here or guide me to a good tutorial with API's and Angular? I looked all over the web to find this and can't find anything that resembles this.

From the doc:

" The $http service is a function which takes a single argument — a configuration object — that is used to generate an HTTP request and returns a promise with two $http specific methods: success and error. ... Since the returned value of calling the $http function is a promise, you can also use the then method to register callbacks, and these callbacks will receive a single argument – an object representing the response. "

$http.get is a shortcut method to perform GET request.

when you call $http.get('/api/1/items/available') you are making a GET request to '/api/1/items/available'. As the request is asynchronous, you have to define a callback function to manipulate the result, and callbacks functions are registered by 'success' and 'then' methods of promise returned by $http call. The 'error' method is used to register error callbacks.

If you are working with a REST api, you can use the Resource angular module .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM