1

我有一个Angular 8应用程序。

app.component.html

 <div>
    <label class="form-check-label f-14">
       <input type="checkbox" class="form-check-input" name="isAgree" [(ngModel)]="isAgree" #isAgree="ngModel"/> Agree?
    </label>
 </div>

但这是抛出错误。

compiler.js:7627 未捕获的错误:无法分配给引用或变量!在 _AstToIrVisitor.visitPropertyWrite (compiler.js:7627)

我想了解这个错误定义和原因。我怎样才能摆脱这个

另外,我不想在我的app.component.ts. 我需要访问isAgree视图中的标志值。所以理想情况下我正在尝试如下。

<div>
    <label class="form-check-label f-14">
       <input type="checkbox" class="form-check-input" name="isAgree"  #isAgree/> Agree?
    </label>
    <p> {{isAgree}} </p> 
 </div>

但价值并没有体现出来。

我怎样才能以这种方式获得价值?

我知道我正在结合 2 个问题,但两者似乎都非常相关。因此就这样做了。

谢谢!

4

2 回答 2

2
 <div>
    <label class="form-check-label f-14">
       <input type="checkbox" class="form-check-input"  [(ngModel)]="isAgree" #isAgree.value="ngModel"/> Agree?
    </label>
 </div>
<div>

上面的代码可以正常工作。

#isAgree指输入元素。你必须绑定到它的 value 属性 #isAgree.value="ngModel

于 2019-09-06T13:21:28.687 回答
1

您收到的错误消息是因为您试图isAgree在模板中分配一个已经在组件中定义的变量。

只要isAgree您的组件中有该属性,以下就是您所需要的。使用两种方式绑定[(ngModel)]将确保在输入值更改时更新此属性(反之亦然):

<div>
  <label class="form-check-label f-14">
    <input type="checkbox" class="form-check-input" name="isAgree" [(ngModel)]="isAgree"/>
    Agree?
  </label>
  <p>{{isAgree}}</p> 
</div>
于 2019-09-06T13:22:01.427 回答