简体   繁体   English

在 Typescript 中设置模型属性的默认值

[英]Set default value of model property in Typescript

I want to set default value of a model property with Typescript.我想用 Typescript 设置模型属性的默认值。

I tried like this using getter and setters but it doesn't changed:我尝试这样使用 getter 和 setter,但它没有改变:

export class Definition extends BaseModel{
    id: number;
    parent_id:number;
    name: string;
    parent_name: string;
    active:boolean;
    get active_text(): string {
        return this.active ? 'Aktif':'Pasif';
    }
    set active_text(value:string) {
        this.active_text = value;
    }

}

I want to set active_text a string by active propery value.我想通过active属性值设置active_text一个字符串。

How can I achieve this?我怎样才能做到这一点?

Things would maybe be clearer and more easily implemented with a backing field to your active_text property :使用active_text属性的支持字段,事情可能会更清晰,更容易实现:

export class Definition extends BaseModel{
    // ... other fields here 

    _active_text: string;
    get active_text(): string {
        // Use this._active_text value unless it is falsy (empty or undefined),
        // in this case use calulated value
        return this._active_text || (this._active ? 'Aktif':'Pasif');
    }
    set active_text(value:string) {
        this._active_text = value;
    }

}

EDIT : another strategy, depending on your usage, would be to trigger the change in active_text when you set the active value, for instance :编辑:根据您的使用情况,另一种策略是在您设置active值时触发active_text的更改,例如:

active_text: string;

_active: boolean;
get active(): boolean{
    return this._active;
}
set active(value: boolean) {
    this._active = value;
    if (!this.active_text) { // the 'if' is here to prevent overriding an existing value)
        this.active_text = this._active ? 'Aktif': 'Pasif'; 
    }
}

The initial values could also be set in the definition or in a constructor, as already noted in another answer.正如在另一个答案中已经指出的那样,初始值也可以在定义或构造函数中设置。

This works for me,这对我有用,

    export class test
    {
    constructor()
    {}

    id: number;
    parent_id:number;
    name: string;
    parent_name: string;
    private  _activeText:string;
    get activeText(): string
    {
        return this._activeText ? 'Aktif':'Pasif';
    }
    set activeText(value:string)
    {
        this._activeText = value;

    }

}
 const  t = new test();
 t.activeText = "value";
console.log(t.activeText);

Did you try to initialize the value?您是否尝试初始化该值?

export class Definition extends BaseModel{
    id: number = 1;
    parent_id:number;
    name: string;
    parent_name: string;
    active:boolean;
    get active_text(): string {
        return this.active ? 'Aktif':'Pasif';
    }
    set active_text(value:string) {
        this.active_text = value;
    }

}

If you want a default value per instance intialize it via the the constructor如果你想要每个实例的默认值通过构造函数初始化它

export class Definition extends BaseModel{
    parent_id:number;
    name: string;
    parent_name: string;
    active:boolean;

    constructor(private id: number) {
    }
    get active_text(): string {
        return this.active ? 'Aktif':'Pasif';
    }
    set active_text(value:string) {
        this.active_text = value;
    }

}

Also notice that you are using your getter to set the value, which will not work;另请注意,您正在使用 getter 来设置值,这是行不通的;

set active_text(value:string) {
        this.active = value;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM