ยท
With Angular 16, you can use the {๐๐๐๐ข๐๐๐๐:๐ก๐๐ข๐} option for an @๐ผ๐๐๐ข๐ก() decorator to make a property mandatory.
This means that the component will only work if you pass a value for that property.
๐จโ๐ป๏ผฃ๏ผฏ๏ผค๏ผฅใ๏ผณ๏ผฎ๏ผฉ๏ผฐ๏ผฐ๏ผฅ๏ผด
// ๐๐๐.๐๐๐๐๐๐๐๐๐.๐๐ import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: ` <h1>Angular 16 Example</h1> <app-greeting [name]="name"></app-greeting> <app-greeting></app-greeting> // This will cause an error ` }) export class AppComponent { name = 'Alice'; } // ๐๐๐๐๐๐๐๐.๐๐๐๐๐๐๐๐๐.๐๐ import { Component, Input } from '@angular/core'; @Component({ selector: 'app-greeting', template: ` <p>Hello, {{name}}!</p> ` }) export class GreetingComponent { @๐๐ง๐ฉ๐ฎ๐ญ({๐ซ๐๐ช๐ฎ๐ข๐ซ๐๐:๐ญ๐ซ๐ฎ๐}) ๐ง๐๐ฆ๐? :๐ฌ๐ญ๐ซ๐ข๐ง๐ ; // This property is mandatory }
โ๏ผฅ๏ผฒ๏ผฒ๏ผฏ๏ผฒ
If the name property of GreetingComponent is not filled, the error message will be something like this:
๐ธ๐๐๐๐: ๐๐๐ ๐ ๐๐๐ ๐๐๐๐ข๐๐๐๐ @๐ผ๐๐๐ข๐ก() ๐๐๐ ๐๐๐๐๐๐๐๐๐ก ๐บ๐๐๐๐ก๐๐๐๐ถ๐๐๐๐๐๐๐๐ก: ๐๐๐๐
#angular #angular16 #requiredInput #programming