import { Component } from '@angular/core'; @Component({ selector: 'app-greet', template: `<h1>Hello, {{name}}!</h1>`, styles: [`h1 { color: blue; }`] }) export class GreetComponent { name = 'Alice'; }
The template uses interpolation {{name}} which Angular replaces with the value of the 'name' property, 'Alice'. The styles apply blue color to the <h1> element.
Inline template is provided with 'template' property as a string. Inline styles are provided with 'styles' as an array of strings. 'templateUrl' and 'styleUrls' are for external files.
import { Component } from '@angular/core'; @Component({ selector: 'app-error', template: `<p>{{message}}</p>` }) export class ErrorComponent { constructor() { console.log(this.message.length); } message: string; }
The 'message' property is declared but not initialized before the constructor runs. Accessing 'this.message.length' causes a runtime error because 'message' is undefined.
The 'selector' defines the custom HTML tag that Angular looks for to insert this component's template. For example, 'app-hello' means <app-hello> in HTML.
import { Component } from '@angular/core'; @Component({ selector: 'app-counter', template: `<button (click)='increment()'>Count: {{count}}</button>` }) export class CounterComponent { count = 0; constructor() { this.increment(); } increment() { this.count += 1; } }
The 'count' starts at 0. The constructor calls 'increment()', which adds 1. So the final value is 1 before any user clicks.