Angular 5 - 如何在组件初始化后重新加载组件

时间:2018-03-06 06:01:28

标签: angular

我在应用程序中有一个搜索功能。当用户点击搜索按钮时,数据在[(ngModel)]中被捕获并传递给服务,URL被重定向到搜索组件的html。 该服务被注入“搜索”组件并显示数据。

现在,当用户输入新的搜索词时,我想刷新现有的搜索组件。这样做的正确方法是什么?

search.component.ts

import { Component, OnInit } from '@angular/core';
import { DataService } from '../service/data.service';

@Component({
selector: 'app-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.css']
})
export class SearchComponent implements OnInit {

userInput : string; 
constructor(private data : DataService) { }

ngOnInit(){
    this.search();
}

search(){

    this.userInput = this.data.searchData;
    console.log('in search init  ' + this.userInput);
    this.data.searchData = "";
 }
}

app.component.ts

import { Component } from '@angular/core';
import { DataService } from './service/data.service';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})



export class AppComponent {
  constructor(private data : DataService){};

  title = 'app';
  searchInput: string;

  searchButtonclick(){
    console.log('search button clicked   '  + (this.searchInput));
    this.data.searchData = this.searchInput
    this.searchInput = "";
  }

}

app.component.html

<form>
  <input type="text" placeholder="Search" [(ngModel)]="searchInput" name="inputfield">
  <a routerLink="/search">
    <button (click)="searchButtonclick()">Search</button>
  </a>
</form>

2 个答案:

答案 0 :(得分:2)

我建议使用search.component.ts作为子组件并使用@Input

<强> app.component.html

<form>
  <input type="text" placeholder="Search" [(ngModel)]="searchInput" name="inputfield">
  <app-search [userInput]="searchInput"></app-search>
</form>

<强> search.component.ts

@Input() userInput: string;

ngOnChange(){
    // this will be called each time userInput changes
    this.search(); 
}

有关详细信息,请参阅this article

如果您想在点击事件中使用,您只需要一个额外的变量,例如searchvalue

<强> app.component.html

<form>
  <input type="text" placeholder="Search" [(ngModel)]="searchInput" name="inputfield">
  <button (click)="searchButtonclick()">Search</button>
  <app-search [userInput]="searchvalue"></app-search>
</form>

<强> app.component.ts

searchvalue:string;
searchButtonclick(){
    this.searchvalue = this.searchInput;
}

<强> search.component.ts

// as above

答案 1 :(得分:1)

好的诀窍是,如果模型更新,则无需重新加载组件。视图会自动更新。

所以我编辑了我的data.service以获得如下的observable,然后在搜索组件中订阅了这个Observable。

用于共享数据的data.service

import { Injectable } from '@angular/core';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';

@Injectable()
export class DataService {

  private localData = new BehaviorSubject<string>("");
  searchData = this.localData.asObservable(); 
  constructor() { }

  updateSearchInput(data : string ){
       this.localData.next(data);
  }
}

search.component

 import { Component, OnInit, Input } from '@angular/core';
 import { DataService } from '../service/data.service';

@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.css']
})
export class SearchComponent implements OnInit {

  userInput : string;
  constructor(private data : DataService) { }

  ngOnInit(){
    this.search();
  }

  search(){    
    this.data.searchData.subscribe((data) => this.userInput = data );  
  }
}