Sure! Here's an example of a web application built using Angular, which includes a form with health-related questions and a moodboard page to summarize the answers.
First, make sure you have Angular CLI installed. If not, run the following command to install it:
npm install -g @angular/cli
Next, create a new Angular project:
ng new health-app
cd health-app
Now, create two components: HealthFormComponent for the form page and MoodboardComponent for the moodboard page.
ng generate component HealthForm
ng generate component Moodboard
Inside the 'src/app' directory, open the health-form.component.ts file and replace the generated code with the following:
import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
@Component({
selector: 'app-health-form',
templateUrl: './health-form.component.html',
styleUrls: ['./health-form.component.css']
})
export class HealthFormComponent {
healthForm: FormGroup;
constructor(private formBuilder: FormBuilder) {
this.healthForm = this.formBuilder.group({
name: ['', Validators.required],
age: ['', Validators.required],
gender: ['', Validators.required],
question1: ['', Validators.required],
question2: ['', Validators.required],
// Add more health-related questions here
});
}
onSubmit() {
// Handle form submission and navigate to moodboard page
}
}
Next, open the health-form.component.html file and replace the generated code with the following:
<form [formGroup]="healthForm" (ngSubmit)="onSubmit()">
<label for="name">Name:</label>
<input type="text" id="name" formControlName="name">
<label for="age">Age:</label>
<input type="number" id="age" formControlName="age">
<label for="gender">Gender:</label>
<select id="gender" formControlName="gender">
<option value="" disabled>Select gender</option>
<option value="male">Male</option>
<option value="female">Female</option>
</select>
<label for="question1">Question 1:</label>
<textarea id="question1" formControlName="question1"></textarea>
<label for="question2">Question 2:</label>
<textarea id="question2" formControlName="question2"></textarea>
<!-- Add more health-related questions here -->
<button type="submit" [disabled]="!healthForm.valid">Submit</button>
</form>
Now, let's implement the moodboard component. Open the moodboard.component.ts file and replace the generated code with the following:
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-moodboard',
templateUrl: './moodboard.component.html',
styleUrls: ['./moodboard.component.css']
})
export class MoodboardComponent {
@Input() formData: any;
constructor() { }
}
Finally, open the moodboard.component.html file and replace the generated code with the following:
<div>
<h2>Moodboard</h2>
<p>Name: {{ formData.name }}</p>
<p>Age: {{ formData.age }}</p>
<p>Gender: {{ formData.gender }}</p>
<p>Question 1: {{ formData.question1 }}</p>
<p>Question 2: {{ formData.question2 }}</p>
<!-- Display more health-related answers here -->
</div>
With all the changes made, update the app.component.html file with the following:
<app-health-form *ngIf="!showMoodboard" (formData)="moodboardData = $event; showMoodboard = true"></app-health-form>
<app-moodboard *ngIf="showMoodboard" [formData]="moodboardData"></app-moodboard>
Now, you can run the application using ng serve and navigate to http://localhost:4200 to see the form. After submitting the form, it will navigate to the moodboard page, displaying the answers.
Note: This is a basic example and can be expanded upon based on your specific requirements.