可在模板中使用的组件实例名称空间/标识

Arl*_*rlo 5 angular

我理解如何使用ElementRef并且应该避免设置元素id,但是仍然存在要设置元素的id属性的情况.

例如,使用输入和标签(由于第三方CSS而无法嵌套):

<input [attr.id]="'myinput'+instanceId" />
<label = [attr.for]="'myinput'+instanceId">My Label</label>
Run Code Online (Sandbox Code Playgroud)

为简单起见,我刚刚增加了这样的数字:

let numInstances = 0;

@Component({

})
export class MyComponent {

  private instanceId: number;

  constructor(){
    this.instanceId = numInstances++;
  }
}
Run Code Online (Sandbox Code Playgroud)

但我想知道是否有可以在模板中使用的组件实例命名空间.像这样的东西?Angular2将用组件实例的唯一字符串替换#.

<input [attr.id]="#myinput" />
<label = [attr.for]="#myinput">My Label</label>
Run Code Online (Sandbox Code Playgroud)

如果这不存在,您认为我应该向Angular2团队提出功能请求吗?

Dar*_*rko 5

在网上阅读了一些讨论后,我决定使用https://github.com/angular/angular/issues/5145#issuecomment-256354115中提出的 lodash 方法

它对我有用,希望对其他人有帮助!