我正在尝试在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();
}]);

最佳答案

您的控制器声明中有问题。您的ProjectService参数与$ scope服务匹配。改为这样做;

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


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

10-07 21:51