我订阅了发射器,我可以在控制台中看到 this.productNumber 的输出。但它在 html 文件中不可见。我像{{productNumber}}
在 html 文件中一样使用它。
constructor(private http : HttpClientService) {
this.http.orderDetailEmitter.subscribe((response)=>{
this.productNumber=response;
console.log("productNumber"+this.productNumber);
});
}
HTML 文件
<div>
<div class="mat-display-1">you have {{productNumber}} product</div>
<mat-form-field floatLabel="never" appearance="legacy" color="accent">
<mat-label> Name</mat-label>
<input matInput required>
<mat-hint align="end">min 5 characters</mat-hint>
</mat-form-field>
</div>
如果需要更多详细信息,请告诉我
服务代码
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { EventEmitter } from '@angular/core';
export class Employee{
constructor(
public empId:string,
public name:string,
public designation:string,
public salary:string,
public isEditable : boolean
) {}
}
@Injectable({
providedIn: 'root'
})
export class HttpClientService {
orderDetailEmitter = new EventEmitter<number>();
constructor(
private httpClient:HttpClient
) {
}
getEmployees()
{
return this.httpClient.get<Employee[]>('http://localhost:8081/fetchEmployees');
}
public deleteEmployee(employee) {
return this.httpClient.delete<Employee>("http://localhost:8081/deleteEmployee" + "/"+ employee.emailid);
}
public createEmployee(employee) {
return this.httpClient.post<Employee>("http://localhost:8081/employees", employee);
}
public updateEmployee(employee){
return this.httpClient.post<Employee>("http://localhost:8081/updateEmployee", employee);
}
public createUser(user){
return this.httpClient.post("http://localhost:8081/registration",user);
}
}