【问题标题】:How is the iteratable id stored in #refernce variable within the div tag using angular2?可迭代 id 如何使用 angular2 存储在 div 标签内的#refernce 变量中?
【发布时间】:2018-10-24 21:09:41
【问题描述】:

我怀疑 user.id 是否分配给了引用变量。我试过了,它不起作用。你能举一个例子来说明它是如何完成的吗?可迭代值如何存储在引用变量中。

【问题讨论】:

  • 别人怎么会知道你的用户对象是由什么组成的?您的问题结构不佳,缺乏基本信息需要回答。
  • 你为什么有ng-template?你能提供更多代码并解释为什么你有一个ng-template吗?这是您要传递给其他组件的模板吗?
  • 如何将 user.id 分配给(#id)。可能或不可能。因为我想获取数据。它的值 print.its 不能存储在 id 中

标签: angular angular2-routing angular2-template angular2-forms


【解决方案1】:

你知道这个模板是什么吗?

它们允许您访问当前模板中的 DOM 元素或组件或指令的实例。它们通过将井号 (#) 与名称一起作为 DOM 元素的附加属性来表示。如果此 DOM 元素只是一个普通元素,则该变量包含对它的引用。在这种情况下,您可以通过此变量访问该 DOM 元素的任何属性。

请参考以下示例:

If you have a component living on the DOM element the variable will contain the instance of that component (and not the DOM element):

<my-component #variable [input]="'hello'"></my-component>
{{ variable.input }}

in your case:
<div *ngFor="let user of users">
<ng-container #id *ngIf="user.method == update.business_type">{{ user.id }}
</ng-container>`
</div>
{{user.id }}

【讨论】:

    【解决方案2】:
    <div *ngFor="let user of users">
    <ng-container [value]=user.id  #id *ngIf="user.method == update.business_type">{{ user.id }}
    </ng-container>
    </div>
    

    有一个值属性存储user id的数据&引用变量存储值的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-20
      • 1970-01-01
      • 2021-06-11
      • 2021-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多