How to get the parent DOM element reference of an angular2 component

前端 未结 5 1861
醉酒成梦
醉酒成梦 2021-01-04 20:32

I need to access to some properties of the parent DOM element which contains the component from where i want to get the info, is there a way to do such thing?

Here i

5条回答
  •  没有蜡笔的小新
    2021-01-04 21:20

    for accessing values of parent you can use @input. for changing parent values try this example

    child component

    import { Component, EventEmitter, Input, Output } from '@angular/core';
    
    @Component({
      selector: 'rio-counter',
      templateUrl: 'app/counter.component.html'
    })
    export class CounterComponent {
      @Input()  count = 0;
      @Output() result = new EventEmitter();
    
      increment() {
        this.count++;
        this.result.emit(this.count);
      }
    }
    

    child html part

    Count: {{ count }}

    parent component

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'rio-app',
      templateUrl: 'app/app.component.html'
    })
    export class AppComponent {
      num = 0;
      parentCount = 0;
    
      assignparentcount(val: number) {
        this.parentCount = val;
      }
    }
    

    parent html part

    Parent Num: {{ num }}
    Parent Count: {{ parentCount }}

提交回复
热议问题