"ng-true-value"和"ng-false-value" Angular2中的替代方法 [英] "ng-true-value" and "ng-false-value" alternatives in Angular2

查看:52
本文介绍了"ng-true-value"和"ng-false-value" Angular2中的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索Angular 2中的ng-true-valueng-false-value替代项,但没有得到结果.他们是否用其他工具代替了它们?我真的很需要他们预先感谢

I was searching for the ng-true-value and ng-false-value alternatives in Angular 2 but i didn't get a result. Did they replace them with other tools ? I really need them. Thanks in advance

推荐答案

解决方法.例如,模型中的对象"带有标志",一个复选框应将标志设置为值"1",另一个复选框应将标志设置为值"2",如果该复选框都未选中,则应将其设置为"0",因此:

Workaround. For example we have the 'object' in the model with a 'flag' and one checkbox should set the flag to value "1" and another checkbox should set the flag to value "2", if the checkbox both unchecked the value should be "0", so:

public setFlag(object: any, event: any): any {
  if (event.target.classList.contains('some-mark-class') && (object.flag == 0 || object.flag == 1)) {
    object.flag = 2;
  } else if (!event.target.classList.contains('some-mark-class') && (object.flag == 0 || object.flag == 2)) {
    object.flag = 1;
  } else {
    object.flag = 0;
  }
}

<input type="checkbox" name="hide-{{object.flag}}" [checked]="object.flag === 1" (change)="setFlag(object, $event)">
<input type="checkbox" name="dlte-{{object.flag}}" [checked]="object.flag === 2" (change)="setFlag(object, $event)" class="some-mark-class">

该解决方案可能并不完美,并且有点笨拙,但这确实可行,因此,如果您有一些改进,请这样做.

The solution might be not perfect and somehow clumsy but it's something and it works, so if you have some improvements, please do.

当我有更多时间时,如果需要的话,我会尝试在一些沙箱中进行:).

When I'll have some more time I'll try to do it in some sandbox if you want :).

更新: 第2个人 更好的方法和灵活性

UPDATE: Verson 2 Better approach and flexibility

.ts文件:

public setFlag(obj: any, property: any, trueValue: any, falseValue: any): any {
  if (obj[property] === trueValue) {
    obj[property] = falseValue;
  } else {
    obj[property] = trueValue;
  }
}

<input type="checkbox" name="hide-{{object.flag}}" [checked]="object.flag === 1" (change)="setFlag(object, 'flag', 1, 0)>
<input type="checkbox" name="dlte-{{object.flag}}" [checked]="object.flag === 1" (change)="setFlag(object, 'flag', 'true-value', 'false-value')>

在函数setFlag中,我们采用4个参数:对象,对象的属性,真值和假值,这使我们可以灵活地设置所需的值,而无需使用标记类.希望对您有帮助

In a function setFlag we take 4 parameters: object, property for the object, true-value and false-value which give us flexibility to set the value what we wish and do it without mark-class. Hope it helps

这篇关于"ng-true-value"和"ng-false-value" Angular2中的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆