handleEditOk(): void {
const params = { ...this.validateForm.value }
this.userService
.updateById(this.userId, params)
.subscribe((res: User) => {
console.log('修改成功:', res)
this.isVisible = false;
const index = this.userList.findIndex(
user => user.id === res.id
)
console.log(res.id)
this.userList[index] = res
})
修改的值为什么都是空的了???
下面是这个类全部代码
import { Component, OnInit } from '@angular/core';
import { UserService } from '../user.service'
import User from '../user.type'
import { NzMessageService } from 'ng-zorro-antd/message';
import { FormGroup, Validators, FormBuilder } from '@angular/forms';
import { Observable } from 'rxjs';
@Component({
selector: 'app-user-list',
templateUrl: './user-list.component.html',
styleUrls: ['./user-list.component.css']
})
export class UserListComponent implements OnInit {
constructor(private userService: UserService, private nzMessageService: NzMessageService,
private fb: FormBuilder) { }
// 数据
userList: User[]
isVisible = false;
userId: number;
isLoading: boolean
validateForm: FormGroup;
// 手机号码的正则
PHONE_NUMBER_REGEXP = /^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\d{8}$/
handleDelete(id: number) {
this.userService.delUser(id).subscribe(res => {
this.userList = this.userList.filter(user => user.id !== id)
})
this.nzMessageService.info('您已删除该员工', { nzDuration: 2000 })
}
handleCancel() {
this.nzMessageService.info('您已取消删除该员工', { nzDuration: 2000 })
}
editUser(id: number): void {
this.userId = id
this.isVisible = true
console.log(id)
this.userService.findById(id).subscribe((user: User) => {
this.validateForm.patchValue(user)
})
}
handleEditCancel(): void {
this.isVisible = false;
}
handleEditOk(): void {
const params = { ...this.validateForm.value }
this.userService
.updateById(this.userId, params)
.subscribe((res: User) => {
console.log('修改成功:', res)
this.isVisible = false;
const index = this.userList.findIndex(
user => user.id === res.id
)
console.log(res.id)
this.userList[index] = res
})
}
ngOnInit() {
this.isLoading = true
this.userService.fetch().subscribe((res: User[]) => {
this.userList = res
this.isLoading = false
})
this.validateForm = this.fb.group({
account: ['admin888', [Validators.required, Validators.minLength(6)]],
email: ['admin@qq.com', [Validators.required, Validators.email]],
password: ['admin', Validators.required],
sex: ['false', Validators.required],
birthday: ['2000-10-10', Validators.required],
phoneNumber: ['13888888888', Validators.pattern(this.PHONE_NUMBER_REGEXP)],
});
}
}