我如何将其写为AngularJS $ http.get?

时间:2016-05-13 21:54:31

标签: angularjs

我在想,怎么能写成angularJS?

requests: {
    fetchUsers: function() {
        return {
            url: 'https://www.example.com/api/v2/users',
            type: 'GET',
            username: 'myemail@email.com/token',
            password: 'myAPItoken'
        };
    }
}

1 个答案:

答案 0 :(得分:2)

这只是一个例子:

你的控制器:

var userCtrl = angular.module('UserCtrl',[]);

    userCtrl.controller('UserController',function($scope,Users){
        var getUsers = Users.fetchAll();
            getUsers.then(function(response){
               $scope.users = response;
            });
    });

您的服务:

var userSrvc = angular.module('UserSrvc',[]);

userSrvc.factory("Users",function($http){

    return{
        fetchAll: function(){
            var request = $http({method:'GET', url:'https://www.example.com/api/v2/users'});
            return request;
        }
    }
});