4

I have defined an Interface in TypeScript like below:

export interface User {
    id?: number;
    name?: string;
    logoUrl?: File;   
    emailUserName?: string;
    emailPassword?: string;
}

With User, I bind it to Angular html input. If I enter anything in the input, the User Object will contains the value, But, If I do not enter, the property like name will be undefine. How could I get empty value for name if I do not enter string for name. Update

<div *ngIf="hero">
<h2>{{hero.name}} details!</h2>
<div>
<label>id: </label>{{hero.id}}</div>
<div>
<label>name: </label>
<input [(ngModel)]="hero.name" placeholder="name" />
<input [(ngModel)]="hero.location" placeholder="location" />
</div>
<button (click)="goBack()">Back</button>
</div> 
//model
    export class Hero {
    private _location:string="test";
    constructor(){
    console.log(this._location);
    this.location=this._location;
    }
    public id: number;
    public name: string;
    public location:string;
    } 

goBack(): void {
console.log(this.hero);
// this.hero=new Hero();
// console.log(this.hero);
//this.location.back();
}
} 

goBack will output hero without location property if I do not enter any value for it in input.

3 Answers 3

8

You can have a class for User instead of an interface, and in the getter do something like:

class User {
    private _name: string;
    ...

    get name() {
        return this._name || "";
    }
}

or even assign an empty string to this._name in the constructor.

If you prefer to use an interface you can have a function that does that:

function normalizeUser(user: User) {
    return Object.assign({ name: "" }, user);
}

Edit

Yes, here's how to set the default value in the ctor:

class User {
    private static DEFAULT_NAME = "";

    public name: string;
    ...

    constructor() {
        this.name = User.DEFAULT_NAME;
    }
}

As for the get name() part, it's an accessor, so if you use it then:

let a = new User();
console.log(a.name);

More on accessors.

Sign up to request clarification or add additional context in comments.

3 Comments

Could I set a default value in class constructor? For your User class, I am not sure how to use "get name()".
Check my revised answer
Thanks, with a click event to run above code, it will the run the constructor, and the default value will be set. But for a model binding in Angular, it will not work. I have updated my post with updated code.
5

Also you can use this shorthand:

class User {
  constructor(public id: number = -1,
    public name: string = '',
    public logoUrl: File = 'defaultLogo.png' ,
    public emailUserName: string = ''
    public emailPassword: string = '') {}
}

3 Comments

It seems it did not work, in constructor, we could not use '?', and even I remove this '?', the result is undefined
sorry, thats true! giving an default value implies '?', so any field with default value is optional. sorry i corrected the code!
I assume your code works when I try something like "let u=new User()", but it may not work when binding with Angular. Please check more information in my original post.
0

Cant you do?

this.user.name?this.user.name:"";

9 Comments

Normally, we pass model from component to service, I am afraid this is not a good solution for checking every property whether they are undefined.
i do not think if there any other solution for checking each property
I'd go with this.user.name || "" instead
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
@CinCout why do you say so
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.