我在ng模板"temp1"中有一个输入定义"nameInput",我想在另一个名为"temp2"的ng模板中读取它的值.我怎么能做到这一点?

<ng-template #temp1 ngx-datatable-cell-template let-rowIndex="rowIndex" let-value="value">
  <input id = "nameInput"
    type="text"
    [value]="value"
  />
</ng-template>
<ng-template #temp2 ngx-datatable-cell-template let-rowIndex="rowIndex" let-row="row">
  {{nameInput.value}}
</ng-template>

推荐答案

遗憾的是,ng模板中的输入元素的值不能直接从另一个ng模板访问.要在两个模板之间传递数据,可以使用共享数据模型.

下面是一个如何做到这一点的例子:

在您的组件中,创建共享数据模型:

export class MyComponent {
  nameInputValue: string;
}

将输入元素的值绑定到nameInputValue属性:

<ng-template #temp1 ngx-datatable-cell-template let-rowIndex="rowIndex" let-value="value">
  <input id="nameInput" type="text" [(ngModel)]="nameInputValue" />
</ng-template>

要仅在nameInputValue具有值时有条件地呈现第二个模板,请使用ngIf:

<ng-template #temp2 ngx-datatable-cell-template let-rowIndex="rowIndex" let-row="row">
  <ng-container *ngIf="nameInputValue">
    {{ nameInputValue }}
  </ng-container>
</ng-template>

确保FormsModule已导入到您的模块中:

import { FormsModule } from '@angular/forms';

@NgModule({
  imports: [FormsModule],
  declarations: [MyComponent],
})
export class MyModule {}

在此方法中,nameInputValue属性在两个模板之间共享,其中一个模板对该属性所做的更改将反映在另一个模板中.

Feel free to modify the code I have provided above if you want. I hope it helps you

Html相关问答推荐

HTML横幅未正确对齐页面顶部

我能阻止浏览器跨跨区边界折叠空格吗?

如何删除在小屏幕上单击按钮时出现的蓝色方块?

将输入字段下方的两个按钮设置为两侧对齐

将2个Flex列合并为1个交替子列

响应CSS中的两到三列布局

滚动两个不同高度的DIV;一个等待另一个

SVG动画只有在打开S开发工具的浏览器时才开始播放

在TWebLabel标题中设置换行符/换行符的方法?

在 HTML 视频上环绕文本叠加

使用 Thymeleaf 将图像水平居中

网页爬虫:使用时光机器进行数据采集

CSS "overflow"不能显示整个单词

如何为Vuetify输入控件减小标签和字段之间的间距?

我正在使用 NVDA 并多次读取关闭按钮,但它的读取非常完美

如何使用 CSS 使粘性元素固定在视口顶部

如何使用 CSS 和 HTML 将文本放入图像中?

无法从社交栏中 Select 选项

尽管设置了 width:100% left:0,但居中对齐的 CSS 动画并不对称

那里有一个类似于 ?