ngoninit

Router Navigate does not call ngOnInit when same page

和自甴很熟 提交于 2019-11-27 03:12:10
I am calling router.navigate on same page with some query string parameters. In this case, ngOnInit() does not call. Is it by default or do I need to add anything else ? Günter Zöchbauer You can inject the ActivatedRoute and subscribe to params constructor(route:ActivatedRoute) { route.params.subscribe(val => { // put the code from `ngOnInit` here }); } The router only destroys and recreates the component when it navigates to a different route. When only route params or query params are updated but the route is the same, the component won't be destroyed and recreated. An alternative way to

Why is ngOnInit called twice?

夙愿已清 提交于 2019-11-26 17:47:45
I trying to create new component, ResultComponent , but its ngOnInit() method is getting called twice and I don't know why this is happening. In the code, ResultComponent inherits @Input from the parent component mcq-component . Here is the code: Parent Component (MCQComponent) import { Component, OnInit } from '@angular/core'; @Component({ selector: 'mcq-component', template: ` <div *ngIf = 'isQuestionView'> ..... </div> <result-comp *ngIf = '!isQuestionView' [answers] = 'ansArray'><result-comp> `, styles: [ ` .... ` ], providers: [AppService], directives: [SelectableDirective,

Angular 2 ngOnInit not called

牧云@^-^@ 提交于 2019-11-26 14:41:25
问题 I am building an Angular 2 app with version beta.8. In this app i have a component which implements OnInit. In this component i have the function ngOnInit, but the ngOnInit function is never called. import { Component, OnInit } from 'angular2/core'; @Component({ templateUrl: '/app/html/overview.html' }) export class OverviewComponent implements OnInit { ngOnInit() { console.log('ngOnInit'); } } The Routing of the app: @RouteConfig([ { path: '/overview', name: 'Overview', component:

Router Navigate does not call ngOnInit when same page

送分小仙女□ 提交于 2019-11-26 10:19:40
问题 I am calling router.navigate on same page with some query string parameters. In this case, ngOnInit() does not call. Is it by default or do I need to add anything else ? 回答1: You can inject the ActivatedRoute and subscribe to params constructor(route:ActivatedRoute) { route.params.subscribe(val => { // put the code from `ngOnInit` here }); } The router only destroys and recreates the component when it navigates to a different route. When only route params or query params are updated but the

Angular 2 Component Constructor Vs OnInit [duplicate]

断了今生、忘了曾经 提交于 2019-11-26 06:29:39
问题 This question already has an answer here: Difference between Constructor and ngOnInit 25 answers If I want function x to happen every time a component loads, whether its the first time, I navigate to a different site and navigate back or it\'s the fifth time the component has loaded. What should I put function x in? The component constructor or OnInit? 回答1: Constructor is predefined default method of the typescript class. There is no relation between Angular and constructor . Normally we use

Difference between Constructor and ngOnInit

给你一囗甜甜゛ 提交于 2019-11-25 21:50:06
问题 Angular provides life cycle hook ngOnInit by default. Why should ngOnInit be used, if we already have a constructor ? 回答1: The Constructor is a default method of the class that is executed when the class is instantiated and ensures proper initialization of fields in the class and its subclasses. Angular or better Dependency Injector (DI) analyzes the constructor parameters and when it creates a new instance by calling new MyClass() it tries to find providers that match the types of the