Angular 16 REQUIRED Input Properties

Angular 16 adds a new feature of making an @Input() decorated property REQUIRED, which means that to use that component, the value of the required property has to be passed.

@Input({required:true}) title? :string;

To Understand it, let us ass…


This content originally appeared on DEV Community and was authored by Dhananjay Kumar

Angular 16 adds a new feature of making an @Input() decorated property REQUIRED, which means that to use that component, the value of the required property has to be passed.

@Input({required:true}) title? :string; 

To Understand it, let us assume that you have a component named ProductComponent with @Input() decorated properties as shown below,

const template = `
   <p>title : {{title}}  price : {{price}} </p>  
`
@Component({
  selector: 'app-product',
  standalone: true,
  imports: [CommonModule],
  template : template
})
export class ProductComponent {
 
  @Input() title? :string; 
  @Input() price? : string; 
 
}

We are using ProductComponent inside AppComponent, as shown below.

import { Component } from '@angular/core';
 
const template = `
<app-product [title]="title"></app-product>
`
@Component({
  selector: 'app-root',
  template : template
})
export class AppComponent {
  title = "Pen"; 
}

As you notice here is that we are,
 
·      Passing value to the title property
·      Not passing value to the price property.
 
In the output, you can see that value of the title is printed, whereas the value of the price is rendered empty.

Image description

We can use the ProductComponent without passing the value of all input decorated properties, and Angular does not complain about that.  Now let us say that we have a requirement that,
 
*The price property is a required property to use the ProductComponent. *
 
Starting Angular 16, we can make an @Input() decorated property as the required property. As you see Input interface has an optional, required boolean property.

Image description

We can use the required property to make an @Input() decorated property required, as shown below,

Image description

You can create a component with a combination of required and optional properties, as shown below,

import { Component, Input } from '@angular/core';
import { CommonModule } from '@angular/common';
 
const template = `
   <p>title : {{title}}  price : {{price}} </p>  
`
@Component({
  selector: 'app-product',
  standalone: true,
  imports: [CommonModule],
  template : template
})
export class ProductComponent {
 
  @Input() title? :string; 
  @Input({required:true}) price? : string; 
 
}

And Angular complains about when you try using the ProductComponent without passing value for price property.

Image description

To use the ProductComponent, you must pass the value of the required @Input() decorated property as shown in the next code block,

import { Component } from '@angular/core';
 
const template = `
<app-product [title]="title" [price]="price"></app-product>
`
@Component({
  selector: 'app-root',
  template : template
})
export class AppComponent {
  title = "Pen"; 
  price = "100"; 
}

This way, you can make an @Input() decorated property as required. I hope you find this post helpful. Thanks for reading.


This content originally appeared on DEV Community and was authored by Dhananjay Kumar


Print Share Comment Cite Upload Translate Updates
APA

Dhananjay Kumar | Sciencx (2023-04-18T04:13:38+00:00) Angular 16 REQUIRED Input Properties. Retrieved from https://www.scien.cx/2023/04/18/angular-16-required-input-properties/

MLA
" » Angular 16 REQUIRED Input Properties." Dhananjay Kumar | Sciencx - Tuesday April 18, 2023, https://www.scien.cx/2023/04/18/angular-16-required-input-properties/
HARVARD
Dhananjay Kumar | Sciencx Tuesday April 18, 2023 » Angular 16 REQUIRED Input Properties., viewed ,<https://www.scien.cx/2023/04/18/angular-16-required-input-properties/>
VANCOUVER
Dhananjay Kumar | Sciencx - » Angular 16 REQUIRED Input Properties. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/04/18/angular-16-required-input-properties/
CHICAGO
" » Angular 16 REQUIRED Input Properties." Dhananjay Kumar | Sciencx - Accessed . https://www.scien.cx/2023/04/18/angular-16-required-input-properties/
IEEE
" » Angular 16 REQUIRED Input Properties." Dhananjay Kumar | Sciencx [Online]. Available: https://www.scien.cx/2023/04/18/angular-16-required-input-properties/. [Accessed: ]
rf:citation
» Angular 16 REQUIRED Input Properties | Dhananjay Kumar | Sciencx | https://www.scien.cx/2023/04/18/angular-16-required-input-properties/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.