简体   繁体   English

Angular 2使用模板引用变量ngForm作为输入绑定

[英]Angular 2 using template reference variable ngForm as input binding

I'm building a component that has an input 'valid'. 我正在构建一个输入'有效'的组件。 If I bind the value to a member of the parent component, things work well. 如果我将值绑定到父组件的成员,事情就会很好。 But if I bind it to a template reference like so 但是,如果我将它绑定到模板引用,就像这样

<step [valid]="name.valid">

      <input type="text" name="name"
             #name="ngForm"
             [(ngModel)]="name"
             required>

</step>

I get 我明白了

Expression has changed after it was checked. 检查后表情发生了变化。 Previous value: 'true'. 上一个值:'true'。 Current value: 'false' 当前价值:'假'

which I partly understand. 我部分理解。 I get that the ngForm valid check occurs after component initialization, and therefore the value has changed. 我得到组件初始化后发生ngForm有效检查,因此值已更改。 What I don't get is why this is a problem, and why this can be solved by calling enableProdMode(), and why enableProdMode() is a bad idea. 我没有得到的是为什么这是一个问题,为什么这可以通过调用enableProdMode()来解决,为什么enableProdMode()是一个坏主意。

I also tried ChangeDetectorRef with .detach() and .reattach() to temporary disable change detection, which didn't solve it and sounds like a bad idea too. 我还尝试使用.detach()和.reattach()的ChangeDetectorRef临时禁用更改检测,但这并没有解决它,听起来也是个坏主意。

Any thoughts? 有什么想法吗?

That's a known issue. 这是一个众所周知的问题。 Use instead 请改用

<form #f="ngForm">
  <step [valid]="f.controls['name'].valid">

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM