2016-02-29 25 views
2

我有调用此函数ngOnInit()中的组分不镖调用angular2

class OtherComponent { 
    // ChromeCast receveiver event handler 
    onLoad() { 
    this.router.navigate(["/Video", { 'pageData': pageData}]); 
    } 
} 

要加载称为video_component.dart另一个镖成分的app_component.dart。

import 'package:angular2/angular2.dart'; 
import 'package:angular2/common.dart'; 
import 'package:angular2/router.dart'; 
import 'dart:js'; 
import 'dart:html'; 


@Component(selector: 'video-container', templateUrl: '../../views/video_component.html', directives: const [NgFor]) 
class VideoComponent implements OnInit { 

    @Input() 
    ContentService contentService; 

    LoggerService log; 

    String progress; 

    RouteParams routeParams; 

    var pageData; 

    VideoComponent(@Inject(ContentService) this.contentService, @Inject(LoggerService) this.log, this.routeParams) { 
    log.info('constructor called'); // <== constructor is called 
    } 

    @override 
    ngOnInit() { 
    log.info('ngOnInit:Load video page with content Id'); 
    //not called 
    } 
} 

为什么不ngOnInit()不会被调用?

+1

'(@Inject(ContentService)'是多余的,如果参数是一个类型的话,这个参数也适用于'this.someField'类型的参数,最后一句看起来不完整 –

+0

yes。 Future()“编译器说'Undefined class Future' –

+0

什么是'onLoad()'事件,它来自哪里? –

回答

2

尝试

NgZone zone; 
ParentComponent(this.zone); 

onLoad(event) { 
    zone.run(() => this.router.navigate(...) 
} 

我以为为父级是一个包含事件处理程序的onLoad(事件)的成分......和它的构造和使用像你和contentService的那样LoggerService注入NgZone中的代码你的问题。将ParentComponent更改为您用于此组件的类的名称。

来自ChromeCast的onLoad()似乎不能从Angular修补,因此不会在Angulars区域运行。现在,当从Angulars区域以外的代码调用事件处理程序时,有些事情不再有效(例如,更改检测)。

随着zone.run(...)我们明确地将背景带回到安格拉斯区域。