我是Angular2和全球网络的新手,我想启动一个操作,在判断checkbox和/或使用Material-Design取消选中时,更改数据库中的项目参数值,我try 了[(ngModel)],但什么都没有发生.我的 idea 是,我必须添加一些状态为checked | unchecked的命题,以判断它是true还是false命题.这是命题模型

export class PropositionModel {
    id:string;
    wordingP:string; // the proposition
    propStatus:Boolean; // the proposition status
}

下面是一个命题的Html代码:

<div class="uk-width-xlarge-1-1 uk-width-medium-1-2">
                <div (submit)="addProp1()" class="uk-input-group">
                    <span class="uk-input-group-addon"><input type="checkbox"  data-md-icheck/></span>
                    <label>Proposition 1</label>
                    <input [(ngModel)]="proposition1.wordingP" type="text" class="md-input" required class="md-input"/>
                </div>
            </div>

下面是用于添加命题的TypeScript代码:

addProp1() {
        this.proposition1 = new PropositionModel();
        this.proposition1.propStatus = false;
        this.propositionService.addProposition(this.proposition1)
            .subscribe(response=> {
                console.log(response);
                console.log(this.proposition1);
                this.proposition1 = new PropositionModel();})
    }

And as you can see i made it a false by default for the proposition status and I want to change it once i checked the proposition. Here is an image how it looks for a better issue understanding. enter image description here

需要帮忙吗?

推荐答案

StackBlitz

Template: You can either use the native 100 event 或 NgModel directive's 101.

<input type="checkbox" (change)="onNativeChange($event)"/>

<input type="checkbox" ngModel (ngModelChange)="onNgModelChange($event)"/>

TS:

onNativeChange(e) { // here e is a native event
  if(e.target.checked){
    // do something here
  }
}

onNgModelChange(e) { // here e is a boolean, true if checked, otherwise false
  if(e){
    // do something here
  }
}

Typescript相关问答推荐

实现忽略了JSSOM中的doctor.body.innerHTML集

排序更改数组类型

TypScript如何在 struct 上定义基元类型?

更新为Angular 17后的可选链运算符&?&问题

打字脚本中泛型和直接类型重新映射的不同行为

状态更新后未触发特定元素的Reaction CSS转换

我可以为情态车使用棱角形的路由守卫吗?

接口中函数的条件参数

是否有一个版本的联合类型递归地使属性只出现在一个可选选项中?

等待用户在Angular 函数中输入

CDK从可选的Lambda角色属性承担角色/复合主体中没有非空断言

如何在使用条件类型时使用void

复选框Angular 在Ngfor中的其他块中重复

将带有样式的Reaction组件导入到Pages文件夹时不起作用

类型脚本-处理值或返回值的函数

tailwind css未被应用-react

为什么在泛型方法中解析此promise 时会出现类型错误?

来自枚举的<;复选框和组件列表

如何在TypeScript类成员函数中使用筛选后的keyof this?

我可以使用对象属性的名称作为对象中的字符串值吗?