第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Angular 2復選框雙向數據綁定

Angular 2復選框雙向數據綁定

HUWWW 2019-12-17 10:49:58
我是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組件中的值,則復選框不會“獲取”新值。因此,我無法從Component中操作復選框(就像我想在Component中的操作一樣)ngOnInit。
查看完整描述

3 回答

?
叮當貓咪

TA貢獻1776條經驗 獲得超12個贊

我喜歡更明確的內容:


component.html


<input #saveUserNameCheckBox

    id="saveUserNameCheckBox" 

    type="checkbox" 

    [checked]="saveUsername" 

    (change)="onSaveUsernameChanged(saveUserNameCheckBox.checked)" />

component.ts


public saveUsername:boolean;


public onSaveUsernameChanged(value:boolean){

    this.saveUsername = value;

}


查看完整回答
反對 回復 2019-12-17
  • 3 回答
  • 0 關注
  • 1534 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號