父组件

    1. <div class="app" >
    2. <app-item *ngFor="let item of list" [data]="item" (click)="handledel(item.id)">
    3. </app-item>
    4. </div>
    1. import { Component, OnInit } from '@angular/core';
    2. import {HttpClient} from '@angular/common/http'
    3. @Component({
    4. selector: 'app-header',
    5. templateUrl: './header.component.html',
    6. styleUrls: ['./header.component.css']
    7. })
    8. export class HeaderComponent implements OnInit {
    9. public list:any;
    10. constructor(public http:HttpClient) { }
    11. ngOnInit() {
    12. var url:string = "http://192.168.14.15:5000/top/playlist?cat=华语";
    13. this.http.get(url).subscribe(res=>{
    14. this.list = res["playlists"];
    15. })
    16. }
    17. handledel(id){
    18. var sub = this.list.filter(item=>item.id!==id)
    19. this.list = sub
    20. }
    21. }

    子组件

    1. <div class="item">
    2. <img [src]="data.coverImgUrl">
    3. <p>{{data.name}}</p>
    4. </div>
    1. import { Component, OnInit,Input} from '@angular/core';
    2. @Component({
    3. selector: 'app-item',
    4. templateUrl: './item.component.html',
    5. styleUrls: ['./item.component.css']
    6. })
    7. export class ItemComponent implements OnInit {
    8. @Input() data:any
    9. constructor() { }
    10. ngOnInit() {
    11. }
    12. }