我在 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;
即使值为真也不会检查。并且点击 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 的情况下实现这种material 方式
isChecked = true;
<md-checkbox
class="example-margin"
[checked]="isChecked"
[indeterminate]="indeterminate"
[align]="align"
(change)="isChecked = !isChecked"
[disabled]="disabled">
I'm a checkbox
</md-checkbox>
关于Angular 4,<md-checkbox> 没有双向绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44553114/