1

I want to declare a variable which should be accessible from the template of the component, but not from other components. If I make it private I will get an error with AoT compilation and if I make it public you know what.

@Component({
  selector: 'my-child',
  template: `
    <div>
      <h2>Hello {{userName}}</h2> 
    </div>
  `,
})
export class ChildComponent{
  public userName = "Test Name"; //declared as public
}
@Component({
  selector: 'my-parent',
  template: `
    <my-child #myChild></my-child>
    <button (click)="buttonClicked"></button>
  `,
})
export class ParentComponent{
  ViewChild('myChild') myChild:ChildComponent;
  public buttonClicked(){
    //I don't want this.myChild.userName be accessible here
  }
}

Is there any way to do so?

Mojtaba
  • 2,764
  • 1
  • 21
  • 24
  • https://stackoverflow.com/q/34574167/6851836 – Hsuan Lee Oct 10 '19 at 11:10
  • @HsuanLee That is not my question. Be more specific if there is a helpful point there please. – Mojtaba Oct 10 '19 at 11:34
  • 1
    You can read my answer here: https://stackoverflow.com/questions/57784610/is-there-a-way-to-restrict-method-variable-access-to-the-component-and-its-temp/57785018#57785018 – Poul Kruijt Oct 10 '19 at 11:57
  • @PierreDuc While the questions was the same as mine, your answer was specific to that scenario. Thanks anyway. – Mojtaba Oct 10 '19 at 13:14
  • 1
    @Mojtaba the code i wrote was specific to that scenario yes, but as stated there, it's not possible :) – Poul Kruijt Oct 10 '19 at 13:50

0 Answers0