角度$inject导致服务失败



每当我的服务中有$inject时,它都会失败。 我似乎找不到错误。 如果我注释掉构造函数上的注入和参数,它可以正常工作。

谢谢

 module app.common {
 export interface IHelloService {
    sayHello(text: string): string;
    sayGoodbye(text: string): string
}
export class HelloService implements IHelloService {
   // lokiInst: Loki;
   // idbAdapter: LokiIndexedAdapter;
   // usersCollection: LokiCollection<starter.domain.User>;
    static $inject = ["$scope"];
    constructor(private $scope:ng.IScope) {
    }

    sayHello(text: string): string {
        return "hello" + text;
    };
    sayGoodbye(text: string): string {
        return "goodbye"+ text;
    };
}

angular.module("app.common", []);
angular
    .module("starter")
    .service("helloService",
    HelloService);
}

不能从服务导入$scope。请参阅有关这一点的文档 - 只有控制器才能访问此内容。

根据你打算做什么(此代码不使用$scope所以很难猜到),你可以导入$rootScope,或者Hello应该是控制器而不是服务。

最新更新