Angular 4,<md-checkbox> 没有双向绑定(bind)

标签 angular checkbox

我在 HTML 中有一个复选框,如下所示,并希望与 ts 中的变量进行双向绑定(bind):

...
        </md-grid-tile>
        <md-grid-tile class="grid-box-row"> 
            <md-checkbox ng-model="item.exportable">{{item.exportable}}
            </md-checkbox>
        </md-grid-tile>        
    </div>
    </ng-container>
</md-grid-list>

item.exportable 是在 ts: 中定义的 bool 值:

exportable: boolean;

显示如下: enter image description here

即使值为真也不会检查。并且点击 UI 上的复选框不会影响复选框后显示的值。请帮忙。谢谢。


谢谢大家的回复。 添加方括号和括号对后:

       </md-grid-tile>
        <md-grid-tile class="grid-box-row"> 
            <md-checkbox [(ng-model)]="item.exportable">{{item.exportable}}
            </md-checkbox>
        </md-grid-tile>        
    </div>
    </ng-container>
</md-grid-list>

我遇到了这个错误:

Unhandled Promise rejection: Template parse errors:
Can't bind to 'ng-model' since it isn't a known property of 'md-checkbox'.
1. If 'md-checkbox' is an Angular component and it has 'ng-model' input, then verify that it is part of this module.
2. If 'md-checkbox' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message.
3. To allow any property add 'NO_ERRORS_SCHEMA' to the '@NgModule.schemas' of this component. ("
        </md-grid-tile>
        <md-grid-tile class="grid-box-row"> 
            <md-checkbox [ERROR ->][(ng-model)]="item.exportable">{{item.exportable}}
            </md-checkbox>
        </md-grid-til

我在 app.module.ts 中导入了 FormsModule:

import {FormsModule} from '@angular/forms';

...

imports: [...
FormsModule,

而且我还在 component.ts 中添加了 FormsModule:

import {FormsModule} from '@angular/forms';

“@angular/material”有什么我需要的吗?

最佳答案

您可以在不使用 ngModel 的情况下实现这种ma​​terial 方式

isChecked = true;

<md-checkbox
          class="example-margin"
          [checked]="isChecked"
          [indeterminate]="indeterminate"
          [align]="align"
          (change)="isChecked = !isChecked"
          [disabled]="disabled">
        I'm a checkbox
      </md-checkbox>

笨蛋 https://plnkr.co/edit/xCpLFKzQnur9Si2KECiW?p=preview

关于Angular 4,<md-checkbox> 没有双向绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44553114/

相关文章:

typescript - HashLocationStrategy 未按预期工作

javascript - 使用复选框 mysql 和 php 或 javascript 隐藏列表中的值

javascript - 使用 React 管理无限滚动表中的复选框状态

javascript - ng-repeat 中的 Angular 多复选框数组

javascript - Angular Material 的样式(css)与文档中的样式不一样

javascript - 如何在 ag-grid 的事件方法中使用 javascript 默认事件对象

angular - 声明 FormBuilder 的库 (@angular/forms) 未被 ngcc 正确处理

javascript - 带有Angular的ASP.NET Core上的级联下拉列表

javascript - jQuery Select Checkbox 当文本存在时,从打印中隐藏未选中的行

c# - 如何在不引发事件的情况下设置 checkbox.isChecked