hex*_*hex 11 javascript angularjs ionic2 ng-upgrade angular
我正在尝试将$log服务用于角度2,根据我的阅读,您需要以下步骤:
所以,我做了以下几点
var initInjector = angular.injector(['ng']);
var $log = initInjector.get('$log');
angular.module('Services1', [])
.service('$log', [$log]);
upgradeAdapter.upgradeNg1Provider('$log');
Run Code Online (Sandbox Code Playgroud)
然后我创建一个角度2组件如下
@Component({ selector: "ion-app", template:"<p>Test</p>" })
@Injectable()
export class HelloIonicPage {
message: string;
constructor( @Inject('$log') $log) {
this.message = "Hi";
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我运行应用程序时,它给出了以下错误:
原始例外:没有提供$ log的提供商!
另外,我试图bootstrap使用upgradeAdapter:
upgradeAdapter.bootstrap(document.documentElement, ['Services1'])
Run Code Online (Sandbox Code Playgroud)
但那也行不通.请注意,我正在使用Ionic 2框架,上面的代码是在里面写的
this.platform.ready().then(() => {
//The code is going here
});
Run Code Online (Sandbox Code Playgroud)
小智 14
您需要将服务注册为提供者.这是我如何将angular1 $状态注入angular 2应用程序:
@NgModule({
providers: [
{
provide: '$state',
useFactory: ($injector: any) => $injector.get('$state'),
deps: ['$injector']
}
]
})
Run Code Online (Sandbox Code Playgroud)
然后代替注射:
@Injectable()
export class RestService {
constructor(@Inject('$state') $state: any) {
$state.go('...');
}
}
Run Code Online (Sandbox Code Playgroud)