How to return a resolved promise from an AngularJS Service using $q?

后端 未结 7 1659
Happy的楠姐
Happy的楠姐 2020-12-05 17:23

My service is:

myApp.service(\'userService\', [
  \'$http\', \'$q\', \'$rootScope\', \'$location\', function($http, $q, $rootScope, $location) {
    var defe         


        
7条回答
  •  佛祖请我去吃肉
    2020-12-05 17:52

    Try this:

    myApp.service('userService', [
        '$http', '$q', '$rootScope', '$location', function($http, $q, $rootScope, $location) {
          var deferred= $q.defer();
          this.user = {
            access: false
          };
          try
          {
          this.isAuthenticated = function() {
            this.user = {
              first_name: 'First',
              last_name: 'Last',
              email: 'email@address.com',
              access: 'institution'
            };
            deferred.resolve();
          };
        }
        catch
        {
            deferred.reject();
        }
    
        return deferred.promise;
      ]);
    

提交回复
热议问题