首页 > 解决方案 > 函数更新联系人未定义,虽然已定义

问题描述

我这里有角码:

@Component({
  selector: 'app-detail-view',
  templateUrl: './detail-view.component.html',
  styleUrls: ['./detail-view.component.css']
})
export class DetailViewComponent implements OnInit {

    detailLable:string; 
    contactId: number;
    contact:Contact = null; 

    constructor(
    private contactService: ContactService,
    private route: ActivatedRoute,
    private location: Location
  ) {}

  ngOnInit(): void {
    this.contactId = +this.route.snapshot.paramMap.get('id');
    if(this.contactId == 0){
        this.contact =  {id : null, firstName : null, surname : null, email: null, phone: null};
        this.detailLable = 'New contact'
    } else {
        this.contactService.getContact(this.contactId).subscribe(contact => this.contact = contact);
        this.detailLable = 'Edit contact with id :' + this.contactId; 
    }
  }

  updateContact( contact: Contact): void{
    this.contact = contact;
    this.detailLable = "Edit contact with id " +contact.id
  }

  goBack(): void {
    this.location.back();
  }

  save():void {
    // this.contact = this.contactService.saveContact(this.contact).subscribe(contact => this.contact = contact,   complete: () => console.log(this.detailLable = this.contact.id));
     this.contactService.saveContact(this.contact).subscribe(contact => updateContact(contact));
  }

  new() : void {
      this.contact =  {id : null, firstName : null, surname : null, email: null, phone: null};
  }
}

当我调用保存时,它返回我未定义 updateContact 方法。有人可以给我一点,如何解决这个问题?看来,方法声明有问题。

标签: javascriptangular

解决方案


updateContact在类上定义,所以this.updateContact应该工作。


推荐阅读