Angular JS服务创建错误

时间:2017-10-16 10:15:27

标签: javascript angularjs

我有一个controller.js文件,我也创建了其他控制器,在那里我使用以下方法创建此服务,但我甚至无法点击该服务。

以下代码用于点击服务: -

  

小组1: -   enrollments.myFunc();   摘录2: -   enrollments.getEnrollments() .then(function() { var x=response.data; alert(x); });   小组3: -   enrollments.getProperty()

请让我知道我的错误在哪里。



helloAjaxApp.service('enrollments', ['$scope', '$http', function($scope, $http) {
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded; charset=utf-8";
    $scope.enrollments={};
    this.myFunc = function () {
        $http({
            url : 'enrollments',
            method : "GET"
        }).then(function(response) {
           $scope.enrollments = response.data;
        });
        return $scope.enrollments;
    }
}]);






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]);






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




0 个答案:

没有答案