服务端交互
表单交互:
参考文档:
https://angular.io/guide/user-input
-
open the root
AppModule
, -
import the
HttpClientModule
symbol from@angular/common/http
, -
add it to the
@NgModule.imports
// app.module.ts: import {NgModule} from '@angular/core'; import {BrowserModule} from '@angular/platform-browser'; // Import HttpClientModule from @angular/common/http import {HttpClientModule} from '@angular/common/http'; @NgModule({ imports: [ BrowserModule, // Include it under 'imports' in your application module // after BrowserModule. HttpClientModule, ], }) export class MyAppModule {}
@Component(...) export class MyComponent implements OnInit { results: string[]; // Inject HttpClient into your component or service. constructor(private http: HttpClient) {} ngOnInit(): void { // Make the HTTP request: this.http.get('/api/items').subscribe(data => { // Read the result field from the JSON response. this.results = data['results']; }); } }
this.http .get('https://jsonplaceholder.typicode.com/posts/1', {observe: 'response'}) .subscribe(res => { console.log(res) })
http .get('/api/items') .subscribe( // Successful responses call the first callback. data => {...}, // Errors will call this callback instead: err => { console.log('Something went wrong!'); } );