Javascript AngularJS服务注入器错误
我正在尝试使用AngularJS的API;我做了一个服务,我现在正试图加载到一个控制器,但我看到一些错误Javascript AngularJS服务注入器错误,javascript,angularjs,api,typescript,Javascript,Angularjs,Api,Typescript,我正在尝试使用AngularJS的API;我做了一个服务,我现在正试图加载到一个控制器,但我看到一些错误 Unknown provider: ApiServiceProvider <- ApiService <- ManageIntranetController 我的控制器如下所示: module Services { export class ApiService { getIntranetItems: (action: string) => an
Unknown provider: ApiServiceProvider <- ApiService <- ManageIntranetController
我的控制器如下所示:
module Services {
export class ApiService {
getIntranetItems: (action: string) => any;
constructor($scope, $http: ng.IHttpService) {
this.getIntranetItems = (action: string) => {
return $http({
method: "GET",
url: "https://localhost:44326/api/intranet/" + action,
headers: { 'Content-Type': 'application/json' }
}).success(function (data) {
$scope.intranetItems = data;
}).error(function (msg) {
alert("ERROR: " + msg);
})
};
}
}
}
/// <reference path="../services/apiservice.ts" />
module Controllers {
export interface IManageIntranetController extends ng.IScope {
}
export class ManageIntranetController {
constructor($scope: IManageIntranetController, ApiService: Services.ApiService) {
console.log(ApiService.getIntranetItems("get"));
}
}
}
//
模块控制器{
导出接口IManageIntranetController扩展了ng.IScope{
}
导出类ManageIntranetController{
构造函数($scope:IManageIntranetController,ApiService:Services.ApiService){
log(ApiService.getIntranetItems(“get”);
}
}
}
您仍然需要向angular注册服务/控制器。例如,您可以这样注册控制器:
angular
.module('app')
.controller('myCtrl', MyController); //where MyController is a TypeScript class
export class ApiService {
getIntranetItems: (action: string) => any;
constructor($http: ng.IHttpService) {
this.getIntranetItems = (action: string) => {
return $http({
method: "GET",
url: "https://localhost:44326/api/intranet/" + action,
headers: { 'Content-Type': 'application/json' }
});
};
}
}
对于服务来说,这有点复杂。您需要在类中添加一个返回实例的静态函数,在向angular注册服务时调用该函数,或者(我个人偏好)在类外创建一个javascript函数,如下所示:
function factory($q: ng.IQService): MyService {
return new MyService($q);
}
angular
.module('app')
.factory('myService', factory);
您的错误意味着它不知道服务“ApiService”。您可能忘记在模块上注册它:
angular.module("myApp", [])
.controller("ManageIntranetController", Controllers.ManageIntranetController)
.service("ApiService", Services.ApiService);
此外,您的服务将无法工作,因为您无法在其中插入$scope
。相反,可以这样做:
angular
.module('app')
.controller('myCtrl', MyController); //where MyController is a TypeScript class
export class ApiService {
getIntranetItems: (action: string) => any;
constructor($http: ng.IHttpService) {
this.getIntranetItems = (action: string) => {
return $http({
method: "GET",
url: "https://localhost:44326/api/intranet/" + action,
headers: { 'Content-Type': 'application/json' }
});
};
}
}
在控制器中:
ApiService.getIntranetItems("get").then((data) => {
$scope.intranetItems = data;
});
您是否已在您的angular module上注册服务?e、 g.
angular.module(“myApp”,[]).service(“ApiService”,Services.ApiService)
此外,您不能将$scope
注入到服务中,该服务仅用于控制器和directives@devqon我没有那样做,但现在我做了,我仍然不断收到同样的错误。。哦,好的,我会改变的。