我对所有有角度的忍者都有一个有趣的问题。你们其中一个人可以解释为什么一个注入器注入的$ rootScope服务无法解决使用来自另一个注入器的$ q服务创建的$ apply / $ digest的承诺。
使用以下打字稿编写的以下测试可以重现这一点:
describe("TestInjection", () => {
let rootScope1 : angular.IRootScopeService;
let q1 : angular.IQService;
let rootScope2 : angular.IRootScopeService;
let q2 : angular.IQService;
beforeEach(() => {
let $injector1 : angular.auto.IInjectorService = angular.injector(["ng"]);
let $injector2 : angular.auto.IInjectorService = angular.injector(["ng"]);
// noinspection TsLint
q1 = $injector1.get("$q");
q2 = $injector2.get("$q");
rootScope1 = $injector1.get("$rootScope");
rootScope2 = $injector2.get("$rootScope");
});
describe("with regard injection and promise testing", () => {
it("promise should be resolved if both services come from the same injector", () => {
let deferred = q1.defer();
let value = false;
deferred.promise.then((v : boolean) => {
value = v;
});
deferred.resolve(true);
rootScope1.$digest();
expect(value).toBeTruthy();
});
it("promise should be resolved if both services come from different injectors", () => {
let deferred = q1.defer();
let value = false;
deferred.promise.then((v : boolean) => {
value = v;
});
deferred.resolve(true);
rootScope2.$digest();
expect(value).toBeTruthy();
});
});
});
我很想理解为什么事情会这样。