简体   繁体   English

如何使用 ngTemplateOutlet 上下文(Angular7)保留双向绑定

[英]How to preserve two-way-binding using ngTemplateOutlet context (Angular7)

I need to setup some configuration forms, which will be shown on screen dynamically.我需要设置一些配置 forms,它将动态显示在屏幕上。 The object behind the logic is deeply nested, so I need to react to its structure (eg using ngFor).逻辑后面的 object 嵌套很深,所以我需要对其结构做出反应(例如使用 ngFor)。 Since content on different layers inside the object can have the same presentation I am trying to outsource input-fields as templates.由于 object 内不同层上的内容可以具有相同的演示文稿,因此我试图将输入字段外包为模板。 When, after some for-loops I call my templated-HTML, passing the actual value using context, two-way-binding for data does not work within the template.当在一些 for 循环之后我调用模板化 HTML 并使用上下文传递实际值时,数据的双向绑定在模板中不起作用。 It seems, that obviously the value is passed and not the reference.看来,显然传递的是值而不是引用。 Passing keys etc. also is no option here since the objects content is complex and dynamic, which is why I need to make input bind from ng-template.传递键等在这里也不是选项,因为对象内容是复杂和动态的,这就是为什么我需要从 ng-template 进行输入绑定。

Eg in component.ts setup an object例如在 component.ts 中设置一个 object

test = {time:"08:00"};

In component.html insert:在组件.html 中插入:

<div>
  {{ test.time }}
  <input type="time" [(ngModel)]="test.time"> <!-- works as expected, value changes on input-->
  <ng-template *ngTemplateOutlet="tmplInputTime; context: { value:test.time }"></ng-template>
</div>

<ng-template #tmplInputTime let-value="value">
    <input type="time" [(ngModel)]="value" > <!-- value not changing on input-->
</ng-template>

What am I missing here?我在这里想念什么? Thanks in advance for help on this one!在此先感谢您的帮助!

Instead of passing the value pass whole object something like而不是传递值传递整个 object 类似

<div>
  {{ test.time }}
  <ng-template *ngTemplateOutlet="tmplInputTime; context: { value:test}"></ng-template>
</div>

<ng-template #tmplInputTime let-value="value">
    <input type="time" [(ngModel)]="value.time" > <!-- value not changing on input-->
</ng-template>

working demo工作演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为Angular 7双向绑定分配方法 - Assign a method to Angular 7 two-way-binding Angular2双向绑定停止工作 - Angular2 Two-Way-Binding stopped working 处理 Angular 5 组件中的模型更改(双向绑定) - Handle Model Change in Angular 5 Component (Two-way-binding) 数组类型对单个值的angular2双向绑定 - angular2 two-way-binding of array-type to single value angular2动态形式与* ngFor,双向绑定[(ngModel)]和表单验证 - angular2 dynamic form with *ngFor, two-way-binding [(ngModel)] and form validation Angular - 使用 ng-template 和 ngTemplateOutlet 时出现值绑定问题 - Angular - Value binding issue while using ng-template and ngTemplateOutlet 如何在带有材料表的 Angular 中使用带有自定义指令的 *ngTemplateOutlet 中的上下文 - How to use context in *ngTemplateOutlet with custom Directive in Angular with Material Tables 使用ngTemplateOutlet的Angular 2+ - Angular 2+ using ngTemplateOutlet 是否有一个生命周期钩子在组件初始化后运行一次,但是在加载双向绑定之后? - Is there a lifecycle hook that runs once after a components initialisation, but also after two-way-binding is loaded? Angular - 如何在 ng-container ngTemplateOutlet 中插入两个组件? - Angular - How to insert two components inside a ng-container ngTemplateOutlet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM