Javascript 角度JS服务创建错误
我已经创建了这个服务。 并使用**enrollments.getProperty()**此声明要求调用此服务,但它不起作用。我是angular JS的新手,请告诉我在哪里出错Javascript 角度JS服务创建错误,javascript,angularjs,Javascript,Angularjs,我已经创建了这个服务。 并使用**enrollments.getProperty()**此声明要求调用此服务,但它不起作用。我是angular JS的新手,请告诉我在哪里出错 var helloAjaxApp=angular.module(“myApp”,[]); helloAjaxApp.service('enrollments',['$scope','$http',function($scope,$http){ $http.defaults.headers.post[“内容类型”]=“应用
var helloAjaxApp=angular.module(“myApp”,[]);
helloAjaxApp.service('enrollments',['$scope','$http',function($scope,$http){
$http.defaults.headers.post[“内容类型”]=“应用程序/x-www-form-urlencoded;字符集=utf-8”;
var注册=空;
注册();
$scope.enrollment=函数(){
$http({
url:'注册',
方法:“获取”
}).然后(功能(响应){
注册=响应。数据;
警报(“登记”);
});
};
返回{
getProperty:函数(){
返回注册;
},
setProperty:函数(值){
注册人数=价值;
}
};
}]);代码>
使用angular.module()
使用angular.module()
Angular有两种定义服务的方法:服务
和工厂
在这里,您可以看到不同之处:
基本区别在于服务就像一个构造函数,所以您不需要从中返回对象,而是使用this
关键字定义属性:
this.getProperty = function () {
return enrollments;
}
当使用factory方法时,is希望返回一个包含公开属性/函数的对象
您正在使用factory语法,因此只需将定义更改为使用factory:
helloAjaxApp.factory('enrollments', [ '$scope', '$http', function ($scope, $http)
Angular有两种定义服务的方法:服务
和工厂
在这里,您可以看到不同之处:
基本区别在于服务就像一个构造函数,所以您不需要从中返回对象,而是使用this
关键字定义属性:
this.getProperty = function () {
return enrollments;
}
当使用factory方法时,is希望返回一个包含公开属性/函数的对象
您正在使用factory语法,因此只需将定义更改为使用factory:
helloAjaxApp.factory('enrollments', [ '$scope', '$http', function ($scope, $http)
您应该选择适当的服务结构,如:
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);
});
您应该选择适当的服务结构,如:
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);
});
控制器代码
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
}
}
LoginService是您必须作为参数传递给控制器的服务名称
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
}
}
控制器代码
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
}
}
LoginService是您必须作为参数传递给控制器的服务名称
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
}
}
在哪里定义模块?var helloAjaxApp=angular.module(“myApp”,[]);您得到的错误是什么?没有错误,它不会只命中服务器。您在哪里定义模块?var helloAjaxApp=angular.module(“myApp”,[]);您遇到的错误是什么?没有错误,只是没有命中服务器它没有调用该服务getEnrollments我更改了方法名称以提高可读性,getEnrollments是您的getProperty。我了解这一点,但仍然无法使用以下代码调用该服务enrollmentService.getEnrollments()。然后(函数(enrollments){您可以在这里使用数据。console.log(enrollments);});我有usd,这也是enrollmentService.getEnrollments()。然后(function(){您可以在这里使用数据。});它没有调用服务getEnrollments我更改了方法名称以提高可读性,getEnrollments是您的getProperty。我了解这一点,但仍然无法使用以下代码调用该服务enrollmentService.getEnrollments()。然后(函数(注册){您可以在此处使用数据。console.log(注册);};我还有一个注册服务。getEnrollments()。然后(函数(){您可以在这里使用数据。});