When I run the application , the console shows all the logs that I have ngOnInit , but the application generates only a skeleton view , without displaying variables of the component and texts from l18n . ngOnInit or not working as it should , because I have to be called in the constructor .
These problems disappear when the second time I click on a link to a component, then everything is loaded as it should. This happens in all components in the application and all applications that builds on angular2 starter gulp .
Why only second click loads the variables to view and calls ngOnInit ( when it is called in the constructor ) ?
export class SettingsDevicesComponent implements OnInit {
**variables**
@Component({
selector: 'as-settings-devices',
templateUrl: 'app/settings-devices/settings-devices.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
styleUrls: ['app/settings-devices/settings-devices.component.css'],
providers: [Communication],
pipes: [TranslatePipe],
directives: [REACTIVE_FORM_DIRECTIVES, NgClass, NgStyle, CORE_DIRECTIVES,ROUTER_DIRECTIVES]
})
constructor(private _cm: Communication,
private _cdRef: ChangeDetectorRef,
private _router: Router,
private _sanitizer: DomSanitizationService,
@Inject(ElementRef) elementRef: ElementRef
) {
this.elementRef = elementRef;
this.ngOnInit();
this.fileUpload = new FormGroup({
color: this.color
});
this.fileName = new FormGroup({
fileNameInput: this.fileNameInput
});
this.settingsName = new FormGroup({
settingsNameInput: this.settingsNameInput
});
}
ngOnInit() {
this.getDeviceData();
this.getZoneData();
}
}
getDeviceData()andgetZoneData()doing? Calling them from constructor orngOnInit()shouldn't make much difference.ngOnInit()from your constructor,this.getDeviceData();this.getZoneData();is executed twice. It would be better to move this code to the constructor if you depend on it being executed there, instead of callingngOnInit()from the constructor.