遗憾的是,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个