Is there a namespace / identifier for the component instance that can be used in templates?

I understand how to use ElementRef , and setting element identifiers should be avoided, but there are situations when you want to set the element id attribute.

For example, with inputs and labels (impossible to nest because of third-party CSS):

<input [attr.id]="'myinput'+instanceId" />
<label = [attr.for]="'myinput'+instanceId">My Label</label>

I just increased a number like this for simplicity:

let numInstances = 0;

@Component({

})
export class MyComponent {

  private instanceId: number;

  constructor(){
    this.instanceId = numInstances++;
  }
}

But I was wondering if there is a Component Instance namespace that I can use in templates. Something like that? Angular2 will replace # with a unique string for the component instance.

<input [attr.id]="#myinput" />
<label = [attr.for]="#myinput">My Label</label>

If this does not exist, do you think I should make a function request for the Angular2 command?

+4
source share

Source: https://habr.com/ru/post/1657412/


All Articles