根据父组件中的值更改更新子组件中的值

Updating value in the child component , on value changes in the parent component

我在 Angular 工作,其中 -

我是如何尝试的

我在下面分享我的代码

父组件

.html

<app-banner [tournamentType]='tournamentType'></app-banner>

.ts

子组件

.ts 文件

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

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

  @Input() tournamentType;

  sportsType : any = 1;



  constructor(private rest : ServicesService) { }

  ngOnInit() {
    console.log("this. is banner page" + this.tournamentType);
    alert('hello');

    this.loadDataFromApi(1);
  }

  loadDataFromApi(sportsType) {

     this.rest.getbanner(this.sportsType).then(res => {
       console.log('>>>$$$$$ banner >>>>>> $$$$$$$$$$');
       console.log('  @Input tournamentType; ====' + this.tournamentType );
       console.log(res);

     })
    console.log(sportsType);
  }
}

说这是你的父组件。

    import { Component, ViewChild, AfterViewInit } from '@angular/core';        

    @Component({
        selector: 'app-root',
        templateUrl: './app.component.html'
    })
    export class AppComponent implements AfterViewInit{

        message = "hello again";
        friends:string[] = [];

        //will add friend to our list of friends array
        add(friend){
            if(friend.value){
                this.friends.push(friend.value);    
                friend.value = "";
            }
            console.log(this.friends);
        }

        ngAfterViewInit() {    
            
        }

    }

父组件UI

    <div>
        <input #friend type="text" placeholder="name of friend" />
        <button type="button" (click)="add(friend)">add friend</button>
    </div>

    <app-to-child message="List of friends" [friends]="friends"></app-to-child>   

现在是子组件

将@Input 装饰器与您希望从父组件接收数据的字段一起使用。

    import { Component, OnInit, Input } from '@angular/core';

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

      @Input() message:string;
      @Input() friends:string[];
      constructor() { }

      ngOnInit() {
      }

      //this will called when data is passed from parent to child.
      ngOnChanges(val){
          console.log("change detected");
          console.log(val);                
      }

   }

里面child.component.html

      <h5>CHILD COMPONENT</h5>
      <p>Message : {{message}}</p>
      <div *ngFor="let friend of friends"> {{friend}}</div>

您可以快速了解 component interactions here

从父组件到子组件的值更改会立即反映出来。但是,您可以在子组件中监听值更改事件。阅读更多关于 ngOnChanges

这是 stackblitz

上的示例

app.component.html

<hello name="{{ name }}"></hello>
<p>
  Start editing to see some magic happen :)
</p>

<app-child [data]="count"></app-child>

app.component.ts

import { Component, OnInit } from "@angular/core";

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"],
})
export class AppComponent implements OnInit {
  name = "Angular";

  count = 0;

  constructor() {}

  ngOnInit(): void {
    setInterval(() => this.updateCount(), 1000);
  }

  updateCount(): void {
    this.count++;
  }
}

child.component.html

<p>{{data}}</p>

child.component.ts

import { Component, OnInit, OnChanges, Input, SimpleChanges } from "@angular/core";

@Component({
  selector: "app-child",
  templateUrl: "./child.component.html",
  styleUrls: ["./child.component.css"],
})
export class ChildComponent implements OnInit, OnChanges {
  @Input() data: any;

  constructor() {}

  ngOnInit() {}

  ngOnChanges(changes: SimpleChanges): void {
    console.log("value changed", this.data);
  }
}