ngoninit

Trigger valueChange with Initialized value - angular2

你说的曾经没有我的故事 提交于 2019-12-09 06:42:26
问题 I'm writing an angular2 application and I'm stuck with something. First of all, I have a select which is bind to a formControl : export class MyComponent implements OnInit { profilesBy: Observable<any[]>; myControl = new FormControl(); constructor(public http: Http) { this.profilesBy = this.myControl.valueChanges .map(text => new formatQuery(text.value)) .switchMap(body => this.getGroupBy(body, this.url), (_, res)=> res.json().aggregations.group_by_type.buckets); } } so, myControl is the

How to call a function eveytime a component is opened?

一笑奈何 提交于 2019-12-08 09:06:42
问题 I have an app which connects to a firebase database and download all items then the items are displayed in tables like this: This table is only shown if Im logged in but as soon as I move to another component and return to the one with the tables, the data is not loaded anymore. I think that's because I have the following code in onInit() function: ngOnInit() { this.taskService.getTasks().subscribe(tasks => { this.tasks = tasks; }); } Which basically just read the data and add it to an array.

How do I run a function on ngOnInit() only once and not again upon returning back from another routerLink?

折月煮酒 提交于 2019-12-06 14:40:25
问题 HomeComponent ngOnInit() { console.log('loaded'); this.retrieveData(); } retrieveData() { // this.dataService.getData().subscribe(...); } I am retrieving data when the component loads. When the user clicks on another routerLink , for example, SettingsComponent and returns back to the HomeComponent , the function of course, is called again because the component has loaded again. But Everytime I return back to the component, it makes the function call again which creates too many unwanted HTTP

How do I run a function on ngOnInit() only once and not again upon returning back from another routerLink?

五迷三道 提交于 2019-12-04 20:13:26
HomeComponent ngOnInit() { console.log('loaded'); this.retrieveData(); } retrieveData() { // this.dataService.getData().subscribe(...); } I am retrieving data when the component loads. When the user clicks on another routerLink , for example, SettingsComponent and returns back to the HomeComponent , the function of course, is called again because the component has loaded again. But Everytime I return back to the component, it makes the function call again which creates too many unwanted HTTP requests. I need to prevent this and make sure the function is called only the first time. How do I do

Trigger valueChange with Initialized value - angular2

柔情痞子 提交于 2019-12-03 09:08:33
I'm writing an angular2 application and I'm stuck with something. First of all, I have a select which is bind to a formControl : export class MyComponent implements OnInit { profilesBy: Observable<any[]>; myControl = new FormControl(); constructor(public http: Http) { this.profilesBy = this.myControl.valueChanges .map(text => new formatQuery(text.value)) .switchMap(body => this.getGroupBy(body, this.url), (_, res)=> res.json().aggregations.group_by_type.buckets); } } so, myControl is the formControl , and profilesBy is an Observable of array. The formatQuery just format a body for the query

Angular 4 ngOnInit not called after router.navigate

喜你入骨 提交于 2019-12-03 06:06:21
I have 3 tabs in which one tab shows a table with list of employees. Works good when it is loaded the first time.ngOnInit Fetches data from server using http get. After that when I click add new employee to open a form, which take input from user and when that submit is clicked I call a function which calls the http post service to post that data to my server where it inserts the records and then after that it is redirected back to employee component, but now that employee component was already loaded, I cannot see the new record that I inserted in table unless I recompile my code. employee

What's the difference between ngOnInit and ngAfterViewInit of Angular2?

有些话、适合烂在心里 提交于 2019-12-03 05:26:06
问题 I can not understand what the difference between ngOnInit and ngAfterViewInit . I found the only difference between them is @ViewChild . According to the following code, the elementRef.nativeElement in them are the same. What scene should we use ngAfterViewInit ? @Component({ selector: 'my-child-view', template: ` <div id="my-child-view-id">{{hero}}</div> ` }) export class ChildViewComponent { @Input() hero: string = 'Jack'; } ////////////////////// @Component({ selector: 'after-view',

What's the difference between ngOnInit and ngAfterViewInit of Angular2?

≡放荡痞女 提交于 2019-12-02 19:59:50
I can not understand what the difference between ngOnInit and ngAfterViewInit . I found the only difference between them is @ViewChild . According to the following code, the elementRef.nativeElement in them are the same. What scene should we use ngAfterViewInit ? @Component({ selector: 'my-child-view', template: ` <div id="my-child-view-id">{{hero}}</div> ` }) export class ChildViewComponent { @Input() hero: string = 'Jack'; } ////////////////////// @Component({ selector: 'after-view', template: ` <div id="after-view-id">-- child view begins --</div> <my-child-view [hero]="heroName"></my-child

Angular 2 ngOnInit is not called when routerlink changes

自作多情 提交于 2019-12-01 01:03:02
i have a menu bar that contain a list of menus loaded from express api and every menu is referenced to a page that have an alias wich will be the URL of that page i am trying to load the page when i click to menu, it's done but only when i refresh the page this my menu code export class MenuList implements OnInit { menus:Menu[]; menu:Menu; page:Page; constructor(private router:Router, private menuService:MenuService) { } getMenus():void { this.menuService.getMenus() .subscribe( menus => this.menus = menus, //Bind to view err => { // Log errors if any console.log(err); } ); } getPage(id):void {

Angular 2 ngOnInit not called

夙愿已清 提交于 2019-11-27 15:26:42
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: OverviewComponent }, { path: '/login', name: 'Login', component: LoginComponent }, { path: '/register', name: