【问题标题】:How to insert data to web API using Angular 5如何使用 Angular 5 向 Web API 插入数据
【发布时间】:2018-08-08 12:22:02
【问题描述】:

我是 Angular 的新开发者。我有一个 api 服务。但是当我插入数据时,我在我的表上添加了空数据。我的屏幕上的 id 增加了,但数据没有添加到我的 api 中。我在哪里弄错了?我分享了我的代码。还有机会从下拉列表中获取城市信息。你能帮我解决这个问题吗? 我可以将我的项目添加到您在 Angular 5 或 6 中的答案中。

-我的银行模型

export class {
  id: string;
  name: string;
  address: string;
  contactName: string;
  contactSurname: string;
  contactPhone: string;
  secondContactPhone: string;
  bankLogo: any;
  town: Town;
  city: City;
}

-我的城市模型

export class City{
  id: number;
  name: string;
  code: number;
}

-我的银行服务

import { Injectable } from '@angular/core';
import { HttpClient/*,HttpHeaders */} from "@angular/common/http";
import { of } from 'rxjs/observable/of';
import { Bank } from "../models/bank";
import { Observable } from '../../node_modules/rxjs/Observable';
import { catchError,map,tap } from 'rxjs/operators';
@Injectable()
export class BankService {

      private bankURL = 'exampleURL';
constructor(private http:HttpClient) { }


addContent(newContent:Bank):Observable<Bank>{

  return this.http.post<Bank>(this.bankURL,newContent).pipe(
    tap((bank:Bank) => console.log('inserted bank = ${JSON.stringify(bank)}')), // $json.stringify does not work
    catchError(error=> of(new Bank()))
  );
}
}

-我的银行组件

import { Component, OnInit } from '@angular/core';
import { Bank } from '../models/bank';
import { BankService } from './bank.service';

@Component({
  selector: 'app-bank',
  templateUrl: './bank.component.html',
  styleUrls: ['./bank.component.css'],
  providers:[BankService]
})
export class BankComponent implements OnInit {
  title="Bank";
  bank:Bank[];
  constructor(private bankService:BankService) { }

  ngOnInit() {

  }
  add(  id: string,
    name: string,
    address: string,
    contactName: string,
    contactSurname: string,
    contactPhone: string,
    secondContactPhone: string,
    bankLogo: any):void{

      const newUser : Bank = new Bank();
      newUser.name=name;
      newUser.address=address;
      newUser.contactName=contactName;
      newUser.contactSurname=contactSurname;
      newUser.contactPhone=contactPhone;
      newUser.secondContactPhone=secondContactPhone;
      newUser.bankLogo=bankLogo;
      this.bankService.addContent(newUser).subscribe(insertedUser=>{this.bank.push(insertedUser);});
    }  
}

-Component.html

<div style ="text-align: left;">
 <table style="width: 300px">
   <tr>
     <td>new name:</td>
     <td><input #name placeholder="name"></td>
   </tr>
 </table>
</div>
<tr>
  <button (click)="add(name.value);name.value='';">add</button>
</tr>

【问题讨论】:

  • 你想在newContent发布到post调用之前改变它吗,如果没有那么直接做一个map(res=&gt;console.log(res)),否则如果绝对必要的话,然后把这行console.log('inserted bank = ${JSON.stringify(bank)}')改成@987654329 @
  • ı 将 console.log('inserted bank = ${JSON.stringify(bank)}') 更改为 console.log('inserted bank',bank) 但我将在哪里写 map(res= >console.log(res)) ?
  • 嗨,看看@Emre Sert下面的答案

标签: angular rest httpclient


【解决方案1】:
    return this.http.post<Bank>(this.bankURL,newContent).pipe(
    map((res) => console.log('inserted bank', res)), // $json.stringify does not work
    catchError(error=> of(new Bank()))
  );

如果您没有任何需要更改的数据,那么这就足够了。

【讨论】:

    猜你喜欢
    • 2018-10-28
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多