Angular2 upgradeAdapter无法使用$ rootScope,$ state等

时间:2016-03-17 17:51:38

标签: angularjs typescript angular upgrade

我试图在Angular2的upgradeAdapter的帮助下升级Angular1应用程序。该应用程序使用upgradeAdapter进行自举,并按预期工作,至少只要我不尝试升级A1的集成服务之一。

假设我用打字稿写了这个A1服务:

//A1.ts
// inject decorator that in short does target.$inject = ...injects
@Inject('$rootScope')
class TestService{
    constructor($rootScope: angular.IRootScopeService){
        //$rootScope.whatever();
    }
} 
// register the service in application module
globalModule.service('TestService', TestService);

我可以在任何需要的地方以正常的A1方式注入TestService,并且它可以工作。

现在进行A2升级:

//upgradeAdapter.ts
import {UpgradeAdapter} from 'angular2/upgrade';
const upgradeAdapter = new UpgradeAdapter();
export default upgradeAdapter;

//A2.ts
import upgradeAdapter from './upgradeAdapter'
import { Injectable, Inject} from 'angular2/core'

@Injectable()
export class A2TestService{
    constructor(@Inject('$rootScope') _rootScope: angular.IRootScope){
        //_rootScope.whatever()
    }
}

upgradeAdapter.upgradeNG1Provider('$rootScope');
// tried with additional upgradeAdapter.upgradeNG1Provider('$rootScopeProvider');
upgradeAdapter.addProvider(A2TestService);
// make this service available in A1 World
globalModule.service('A2TestService',upgradeAdapter.downgradeNG2Provider(A2TestService));

混合DI在使用此公式进行升级时有效,只要我不使用,例如上面的$rootScope。 然后我得到了

"Error during instantiation of $rootScope!  (A2TestService -> $rootScope)
Stacktrace: Type Error: Cannot read property 'get' of null 
 at provider.push.core_1.provide.useFactory (upgrade.js: 684)
..."

来自angular2.js(在beta.9上)

我有什么选择?

0 个答案:

没有答案