Q1. How do you create your first Angular component?
Use the Angular CLI to generate a component: ) in another template to display it.
ng generate component my-first-component
This creates a folder with the component''s TypeScript, HTML, CSS, and test files. The component is automatically declared in the nearest module. You can then use its selector (e.g., Q2. What is the basic structure of an Angular component?
A component consists of: a TypeScript class decorated with @Component, a template (HTML), and styles. Example:
@Component({
selector: ''app-root'',
templateUrl: ''./app.component.html'',
styleUrls: [''./app.component.css'']
})
export class AppComponent {
title = ''my-first-app'';
}
The class contains properties and methods that the template can use.Q3. How do you display data in the template?
Use interpolation (double curly braces) to display component properties. In your component:
export class AppComponent {
message = ''Hello Angular'';
}
In the template: {{ message }}
Angular evaluates the expression and converts it to a string.Q4. How do you handle user events in Angular?
Use event binding with parentheses. Define a method in the component:
export class AppComponent {
onClick() {
console.log(''Button clicked'');
}
}
In the template:
Q5. How do you style an Angular component?
Angular supports multiple styling approaches: inline styles using the styles property in @Component, external CSS files via styleUrls, or using the style tag in the template. Styles are component-scoped by default thanks to Angular''s view encapsulation, which prevents styles from leaking to other components.
