【问题标题】:Update user profile is not working angular更新用户配置文件不起作用
【发布时间】:2020-08-31 14:01:19
【问题描述】:

我将 Angular 与 spring-boot 一起使用。我尝试从设置页面更新用户详细信息,但是当单击更新按钮时没有任何反应,所以有人可以让我知道问题出在哪里。如果您要问的是更新功能是否适用于后端,是的,它可以正常工作,所以问题出在前端。

app.routing

 { path: 'profile-update/:id', component: ProfileUpdateComponent },

注册数据

export interface SignUpData {
    id: string;
    username: string;
    email: string;
    nom: string;
    prenom: string;
    telephone: number;
    roles: string[];
    password: string;
    specialite: string;
    adresses: string[];

  }

user.service

  const API_URL = 'http://localhost:8080/user/';

    const httpOptions = {
      headers: new HttpHeaders({ 'Content-Type': 'application/json' })
    };
adresses: string[];
          updateProfile(id: string, userData: SignUpData): Observable<any> {
            return this.http.put(API_URL + 'update/' + id, userData, httpOptions);
          }

update.component.ts

  updateProfile() {
    const {adresse1, ...rest} = this.form;
    const userData: SignUpData = {...rest,  adresses: [adresse1]};
    this.userservice.updateProfile(this.id, userData).subscribe(
      data => {
        console.log(data);
      },
      err => {
        this.errorMessage = err.error.message;
      }
    );
  }
  onSubmit() { 
    this.updateProfile();
    this.gotoList();   
  }

  gotoList() {
    this.router.navigate(['profile']);
  }

user.html

<form
        name="form"
        (ngSubmit)="f.form.valid && onSubmit()"
        #f="ngForm"
        novalidate>
        <div class="form-row">
            <div class="col form-group">
                <label>First name </label>   
                <input type="text" class="form-control" placeholder=""
                name="prenom"
                [(ngModel)]="form.prenom"

                #prenom="ngModel"> 
            </div> <!-- form-group end.// -->
            <div class="col form-group">
                <label>Last name</label>
            <input type="text" class="form-control" placeholder=" "
            name="nom"
                [(ngModel)]="form.nom"

                #nom="ngModel">
            </div> <!-- form-group end.// -->
      </div> <!-- form-row end.// -->


        <div class="form-group">
            <button type="submit" class="btn btn-primary btn-block"> Update </button>
        </div> <!-- form-group// -->      


      </form>

【问题讨论】:

    标签: angular typescript spring-boot spring-mvc


    【解决方案1】:

    将点击事件绑定到更新按钮

    <button type="submit" (click)="updateProfile()" class="btn btn-primary btn-block"> Update </button>
    

    【讨论】:

    • 感谢您的回复我测试它也不起作用 btw 我已经添加 (ngSubmit)="f.form.valid && onSubmit()" 它做同样的工作
    • 我在您的 html 或打字稿中没有看到问题 stackblitz.com/edit/… 刚刚创建了一个 stackblitz 并检查了.. 但可能是服务的问题,我猜您可以创建一个 stackblitz 并共享代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    相关资源
    最近更新 更多