我对Angular2很陌生,我有一个小问题:

在我的Login-Component-HTML中,我有两个复选框,我想以两种数据绑定的方式绑定到Login-Component-TypeScript。

这是HTML:

<div class="checkbox">
<label>
    <input #saveUsername [(ngModel)]="saveUsername.selected" type="checkbox" data-toggle="toggle">Save username
</label>
</div>

这是Component.ts:

import { Component, OnInit }    from '@angular/core';
import { Router }               from '@angular/router';
import { Variables }            from '../../services/variables';

@Component({
    selector: 'login',
    moduleId: module.id,
    templateUrl: 'login.component.html',
    styleUrls: ['login.component.css']
})


export class LoginComponent implements OnInit {

    private saveUsername: boolean = true;
    private autoLogin: boolean = true;
    constructor(private router: Router, private variables: Variables) { }

    ngOnInit() { 
        this.loginValid = false;
        // Get user name from local storage if you want to save

        if (window.localStorage.getItem("username") === null) {
           this.saveUsername = true;
           this.autoLogin = true;
           console.log(this.saveUsername, this.autoLogin);
        } else {
           console.log("init", window.localStorage.getItem("username"));
        }
    }

    login(username: string, password: string, saveUsername: boolean, autoLogin: boolean) {
        this.variables.setUsername(username);
        this.variables.setPassword(password);
        this.variables.setIsLoggedIn(true);
        console.log(saveUsername, autoLogin);
        //this.router.navigate(['dashboard']);
    }

如果我单击复选框,我将在控制器(组件)中获得正确的值。

但是,如果我在组件中更改了例如saveUsername的值,复选框就不会“获得”新值。

所以我不能从组件中操作复选框(就像我想在组件中的ngOnInit中做的那样。

谢谢你的帮助!


当前回答

我正在使用Angular5,我必须添加"name"属性来让绑定工作…绑定时不需要“id”。

<input type="checkbox" id="rememberMe" name="rememberMe" [(ngModel)]="rememberMe">

其他回答

要让复选框工作,你应该遵循以下步骤:

在你的模块中导入FormsModule 将输入放在表单标记中 你的输入应该像这样: <input name="mpf" type="checkbox" [(ngModel)]="value" /> 注意:不要忘记输入姓名。

不幸的是,@hakani提供的解决方案不是双向绑定。它只处理从UI/FrontEnd部分单向更改模型。

而是简单的:

<input [(ngModel)]="checkboxFlag" type="checkbox"/>

将为复选框做双向绑定。

之后,当模型checkboxFlag从后端或UI部分更改时——瞧,checkboxFlag存储实际的复选框状态。

为了确保我已经准备了Plunker代码来显示结果:https://plnkr.co/edit/OdEAPWRoqaj0T6Yp0Mfk

为了完成这个回答,你应该把import {FormsModule}从'@angular/forms'包含到app.module.ts中,并添加到imports数组中

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

[...]

@NgModule({
  imports: [
    [...]
    FormsModule
  ],
  [...]
})

在Angular的p-复选框中,

使用p-复选框的所有属性

<p-checkbox name="checkbox" value="isAC" 
    label="All Colors" [(ngModel)]="selectedAllColors" 
    [ngModelOptions]="{standalone: true}" id="al" 
    binary="true">
</p-checkbox>

更重要的是,不要忘记包括[ngModelOptions]="{standalone: true}以及它挽救了我的一天。

在angular上使用<abc [(bar)]="foo"/>语法时。

这句话的意思是: <abc [bar]="foo" (barChange)="foo = $event" />

这意味着你的组件应该有:

@Input() bar;
@Output() barChange = new EventEmitter();
Angular: "9.0.0"
Angular CLI: 9.0.1
Node: 13.10.1
OS: linux x64

. html文件

<input [(ngModel)]="userConsent" id="userConsent" required type="checkbox"/> " I Accept"

.ts文件

userConsent: boolean = false;