2017-10-16 60 views
1

我已經創建了此服務。 並使用** enrollments.getProperty(); **這個語句來調用這個服務,但它不工作我是新的角JS,請讓我知道我犯了什麼錯誤。Angular JS服務創建錯誤

var helloAjaxApp = angular.module("myApp", []); 
 
helloAjaxApp.service('enrollments', [ '$scope', '$http', function ($scope, $http) { 
 
\t 
 
\t $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=utf-8"; 
 
    var enrollments = null; 
 
    enrollment(); 
 
    $scope.enrollment=function() { 
 
    \t $http({ 
 
    \t url : 'enrollments', 
 
     method : "GET" 
 
    }).then(function(response) { 
 
     
 
    \t enrollments = response.data; 
 
     alert("enrollments"); 
 
    }); 
 
    }; 
 
    return { 
 
     getProperty: function() { 
 
      return enrollments; 
 
     }, 
 
     setProperty: function(value) { 
 
     \t enrollments = value; 
 
     } 
 
    }; 
 
}]);

+0

其中定義模塊? –

+0

var helloAjaxApp = angular.module(「myApp」,[]); – RD063

+0

你得到的錯誤是什麼? – Nitheesh

回答

0

使用angular.module()

(function() { 
    'use strict'; 
    angular.module("helloAjaxApp") 
     .service('enrollments', ['$scope', '$http', function ($scope, $http) { 
      $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=utf-8"; 
      var enrollments = null; 
      enrollment(); 
      $scope.enrollment = function() { 
       $http({ 
        url: 'enrollments', 
        method: "GET" 
       }).then(function (response) { 

        enrollments = response.data; 
        alert("enrollments"); 
       }); 
      }; 
      return { 
       getProperty: function() { 
        return enrollments; 
       }, 
       setProperty: function (value) { 
        enrollments = value; 
       } 
      }; 
     }]); 
}); 
0

角具有2種方式定義服務的:servicefactory

在這裏你可以看到其中的差別:https://blog.thoughtram.io/angular/2015/07/07/service-vs-factory-once-and-for-all.html

的基本區別是,服務就像是一個構造函數,這樣你就不會從它返回一個對象,但是可以定義使用this關鍵字屬性:

this.getProperty = function() { 
    return enrollments; 
} 

使用工廠方法時,需要使用公開的屬性/函數返回對象。

您使用出廠語法,所以只需更改定義中使用的工廠:

helloAjaxApp.factory('enrollments', [ '$scope', '$http', function ($scope, $http) 
0

你應該去一個適當的服務結構,像這樣:

var helloAjaxApp = angular.module("myApp", []); 

function EnrollmentService($scope, $http) { 
    let _this = this; 
    this.enrollments = null; 
    this.getEnrollments = function() { 
     return $http({ 
      url: 'enrollments', 
      method: 'GET' 
     }).then(function(response) { 
      _this.enrollments = response.data; 
      return _this.enrollments; 
     }) 
    }; 

    this.setEnrollments = function(enrollments) { 
     _this.enrollments = enrollments; 
    } 
} 

helloAjaxApp.service('enrollments', ['$scope', '$http', EnrollmentService]); 

然後,使用該服務其他地方:

enrollmentService 
    .getEnrollments() 
    .then(function(enrollments) { 
     // You can use the data here. 
     console.log(enrollments); 
    }); 
+0

其不調用服務getEnrollments – RD063

+0

我改變了方法名以提高可讀性,getEnrollments是你的getProperty。 –

+0

我明白,但仍然不能夠調用服務,使用下面的代碼enrollmentService .getEnrollments() 。然後(函數(入學){ 你可以在這裏使用數據 的console.log(招生); }) ;我有這個也註冊服務 .getEnrollments() .then(function(){ 您可以在這裏使用的數據 }); – RD063

0

控制器代碼

login服務是你必須作爲參數傳遞服務名稱到控制器

var loginModule = angular.module('LoginModule',[]); 
loginModule.controller('logincontroller', ['$rootScope','$scope','$http','$window','$cookieStore', 
                'LoginService',logincontrollerFun ]); 

function logincontrollerFun($rootScope, $scope, $http, $window,$cookieStore, LoginService,RememberService) { 



    $scope.loginTest = function() { 

     LoginService.UserStatus($scope, function(resp) { 
      console.log("response of login controller ::: ", resp); 
      ///write ur code 

     }); 
    } 
} 

服務代碼

var loginModule = angular.module('LoginModule') 
loginModule.factory("LoginService",[ '$http', LoginServiceFun ]) 

    function LoginServiceFun($http) { 
     function UserStatus($scope,callback){ 

       var targetRequestPath='./url'; 
       var targetRequestParamsREQ={'email':$scope.email,'password':$scope.passWord}; 
      return $http({ 
       method: 'POST', 
       url: targetRequestPath, 
       headers: {'Content-Type': 'application/json'}, 
       data: targetRequestParamsREQ 
      }).then(function (response){ 
       console.log('Response Data : ', response.data); 
       callback(response.data); 
      }) 
     } 
     return { 
      UserStatus:UserStatus 
      } 

     }