Angular JS服务创建错误

时间:2017-10-16 08:41:10

标签: javascript angularjs

我创建了这项服务。 并使用** enrollments.getProperty(); **此语句调用此服务但它不起作用我是angular-JS的新手请让我知道我在哪里犯了错误。



var helloAjaxApp = angular.module("myApp", []);
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;
        }
    };
}]);




4 个答案:

答案 0 :(得分: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;
                }
            };
        }]);
});

答案 1 :(得分:0)

Angular有两种定义服务的方式:servicefactory

在这里你可以看到差异:https://blog.thoughtram.io/angular/2015/07/07/service-vs-factory-once-and-for-all.html

基本区别在于服务就像构造函数,因此您不会从中返回对象,而是使用this关键字定义属性:

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

使用工厂方法时,需要使用公开的属性/函数返回对象。

您正在使用工厂语法,因此只需将定义更改为使用factory:

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

答案 2 :(得分: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);
    });

答案 3 :(得分:0)

控制器代码

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
            }

        }