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