typescript 设置布尔打字稿的默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53373572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:43:23  来源:igfitidea点击:

Set default value of boolean typescript

angulartypescriptboolean

提问by user2004

I have an object:

我有一个对象:

export class ReccurrenceModel {
    fromDate: Date;
    toDate: Date;
    weeklyReccurrence: number;
    state: State;
    isMonday: boolean;
    isTuesday: boolean;
    isWednesday: boolean;
    isThursday: boolean;
    isFriday: boolean;
    fromDateToReturn: Date;
    toDateToReturn: Date;
}

I use it like this

我像这样使用它

  if (this.reccurrenceSelected === true) {
      this.reccurrence.isMonday = this.mondaySelected;
      this.reccurrence.isTuesday = this.tuesdaySelected;
      this.reccurrence.isWednesday = this.wednesdaySelected;
      this.reccurrence.isThursday = this.thursdaySelected;
      this.reccurrence.isFriday = this.fridaySelected;
}

I want to set a default value for them - false because if I do not set them in in UI, they will be undefined and I don't want that.

我想为它们设置一个默认值 - false 因为如果我不在 UI 中设置它们,它们将是未定义的,我不想要那样。

How to set de default value of a boolean in typescript?

如何在打字稿中设置布尔值的默认值?

回答by

undefined, as well as false, are both falsy values that you can test the same way.

undefined,以及false,都是可以用相同方式测试的假值。

But default values are set with

但默认值设置为

export class ReccurrenceModel {
    fromDate: Date;
    toDate: Date;
    weeklyReccurrence: number;
    state: State;
    isMonday = false;
    isTuesday = false;
    ...
    fromDateToReturn: Date;
    toDateToReturn: Date;
}

回答by Pardeep Jain

Doesn't make any big change in UI level you can use either. Both are falsy values for UI.

也不会对您可以使用的 UI 级别进行任何重大更改。两者都是 UI 的假值。

You can set anyone.

你可以设置任何人。

variableName = false 

or

或者

variableName: boolean;

variableNameyou can use either UI consider it as false by default untill you assign its value to true.

variableName您可以使用任一 UI 默认将其视为 false,直到您将其值指定为 true。

回答by CruelEngine

I would add a default constructor and do something like this :

我会添加一个默认构造函数并执行以下操作:

export class ReccurrenceModel {
    fromDate: Date;
    toDate: Date;
    weeklyReccurrence: number;
    state: State;
    isMonday: boolean;
    isTuesday: boolean;
    isWednesday: boolean;
    isThursday: boolean;
    isFriday: boolean;
    fromDateToReturn: Date;
    toDateToReturn: Date;

    constructor(){
      this.isMonday = false;
      this.isTuesday = false;
      this.isWednesday = false;
      this.isThursday = false;
      this.isFriday = false;
    }
}

later i would do something like this : ,

后来我会做这样的事情:,

this.reccurrence = new ReccurrenceModel();

The above line would initialize the required fields. you can confirm this by doing a console.log(this.reccurrence)after calling the constructor

上面的行将初始化所需的字段。您可以通过console.log(this.reccurrence)在调用constructor

回答by Ajish Kumar

You can put

你可以放

      this.isMonday = false;
      this.isTuesday = false;
      this.isWednesday = false;
      this.isThursday = false;
      this.isFriday = false;

these in ngOnInit() method also, this is the method which is called first. to find the difference between ngOnInit() and constructor visit Difference between Constructor and ngOnInit

这些也在 ngOnInit() 方法中,这是首先调用的方法。找到 ngOnInit() 和构造函数的区别访问构造函数和 ngOnInit 的区别

回答by Rohit.007

I would suggest to use gettersand settersin class

我建议在课堂上使用getterssetters

export class ReccurrenceModel {
    fromDate: Date;
    toDate: Date;
    weeklyReccurrence: number;
    state: State;
    isMonday: boolean;
    isTuesday: boolean;
    isWednesday: boolean;
    isThursday: boolean;
    isFriday: boolean;
    fromDateToReturn: Date;
    toDateToReturn: Date;
        ...

        get fromDate() {
            return this.fromDate|| "";
        }
        get isMonday() {
            return this.isMonday|| false;
        }
    }