33

Say I have a component that will display a name property, so it roughly goes like this:

import {Component, Input} from 'angular2/core';

@Component({
  selector: 'demo',
  template: `<div>{{name}}</div>`,
  styles: [``],
})
export class Demo {
  @Input() name: string;
}

The problem is, how could I display [noname] when someone using this component but not passing any name property?

The only solution comes to mind is using logic operator inside template like {{ name || '[noname]' }}.

6 Answers 6

55

try

@Input() name: string = 'noname';
Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't work for me. It used the default value EVEN IF I set it to another value in the template.
3

with angular 8 and the default tslint settings your IDE will notice:

enter image description here

so it´s okay to just write:

@Input() addclass = '';

without any "type annotation".

2 Comments

Removing the type annotation makes it work. But why? Shouldn't it not matter?
It doesn't matter. that's a linting suggestion that's only there to standardize your code and make it more readable. you can disable the rule in tslint if you prefer to keep the type annotation.
2

In the component you should initialize like:

@Input () name:String='';

In the HTML you can use:

{{ name ===''?  'empty string': name }}

Comments

2

You can intercept @Input() with a setter and have it backed by a private field. In setter, you do a nullcheck so field gets set only to a non-null value. As last, you bind your template to a private fied in which you have set initial value.

Comments

0

I think you can use your idea of using the template. So it would be:

In Component:

@Input () name:String;

In Template:

<div>{{ name != '' ? name : '[no name]' }}</div>

That would check to see if the name is empty, and use '[no name]' or insert the name if the name is passed.

Comments

0

Here is the proper solution to this. (ANGULAR 2 to 9)

Addressing solution: To set a default value for @Input variable. If no value passed to that input variable then It will take the default value.

Example:

I have an object interface named bike:

export interface bike {
  isBike?: boolean;
  wheels?: number;
  engine?: string;
  engineType?: string;
}

You made a component named app-bike where you need to pass the properties of bike with @input decorator of angular. But you want that isBike and Wheels properties must have a default value (ie.. isBike: true, wheels: 2)

export class BikeComponent implements OnInit {
  private _defaultBike: bike = {
    // default isBike is true
    isBike: true,
    // default wheels  will be 2
    wheels: 2
  };

  @Input() newBike: bike = {};

  constructor() {}

  ngOnInit(): void {

   // this will concate both the objects and the object declared later (ie.. ...this.newBike )
   // will overwrite the default value. ONLY AND ONLY IF DEFAULT VALUE IS PRESENT

    this.newBike = { ...this._defaultBike, ...this.newBike };
   //  console.log(this.newBike);
  }
}

For more detailed article refer to this.

Refer Destructuring assignment from here

Comments

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.