Imagine a scenario involving basic crud operations. Within the app.component.html
file, there are multiple input fields and buttons. Upon clicking a button in app.component.html
, the value of an HTML field is sent to the 'other.component.ts' component. After processing (such as adding, subtracting, etc.), the result is displayed back in app.component.html
.
Below is the content of app.component.html:
<a routerLink="posts/">Show Posts</a>
<input type="number" [(ngModel)]="get-one-post-id">
<a routerLink="/post-by-id">Show One Posts</a>
<router-outlet>
</router-outlet>
post-by-id-component.ts
import { Component, OnInit } from '@angular/core';
import { DataService } from '../data.service';
import { Observable } from 'rxjs';
@Component({
selector: 'app-post-by-id',
templateUrl: './post-by-id.component.html',
styleUrls: ['./post-by-id.component.css']
})
export class PostByIdComponent implements OnInit {
posts: object;
constructor(private dataService: DataService) { }
ngOnInit(): void {
// const id = ??
this.GetPost(1);
}
async GetPost(id: number)
{
const response = await this.dataService.Get_A_Post(id);
const dataService = await response.json();
this.posts = dataService;
}
}
post-by-id-component.html
<div *ngFor="let post of posts">
<h3>{{post.title}}</h3>
<p>{{post.body}}</p>
</div>
In the scenario, there is a need to extract the value from the field named get-one-post-id in app.component.html to post-by-id-component.ts [where // const id = ?? is commented]. However, a suitable method for importing this value has not been identified.