无法访问Angular.js服务中的函数

时间:2016-05-12 09:58:34

标签: javascript angularjs

我正在尝试在AngularJS中创建和使用新服务,但是, 我收到以下错误 -

Error message ProjectService.one不是函数

Javascript -

var app = angular.module('app', ['ngRoute']);

app.service('ProjectService', function () {   
    this.one = function one() {
        console.log('test service');
    };
});

app.controller('ProjectsController', ['$scope', function (ProjectService, Test) {
    ProjectService.one();
}]);

2 个答案:

答案 0 :(得分:3)

您的控制器声明中有错误。您的ProjectService参数与$ scope服务匹配。改为做到这一点;

app.controller('ProjectsController', ['$scope', 'ProjectService', 'Test', function ($scope, ProjectService, Test) {
    ProjectService.one();
}]);

服务参数必须与服务数组(相同的数字和顺序)相匹配

答案 1 :(得分:0)

您需要注入ProjectService和其他所需的依赖模块,如下所述:

app.controller('ProjectsController', ['$scope','ProjectService','Test', function ($scope,ProjectService, Test) {
  ProjectService.one();
}]);