【问题标题】:Error: Uncaught (in promise): Error: Cannot assign to a reference or variable错误:未捕获(承诺中):错误:无法分配给引用或变量
【发布时间】:2019-09-06 04:10:57
【问题描述】:

我知道这可能是一个重复的问题,但通过阅读其他解决方案,我无法弄清楚我的问题。因此,如果有人能指出我的错误,我将不胜感激。 HTML 代码:

<div class="jumbotron">
<div class="container">
  <div class="row">
    <div class="col-md-6 offset-md-3">
      <form [formGroup]="contactForm" >
        <div class="form-group">
          <label>Contact Id</label>
          <input type="text"
           formControlName="contactId"
           [(ngModel)]="contactId"
           class="form-control" />
          <!-- <div *ngIf="submitted && contactFormf.contactId.errors" class="invalid-feedback">
                <div *ngIf="contactFormf.contactId.errors.required">Contact Id is required</div>
              </div> -->
        </div>
        <div class="form-group">
          <label>Agreement Id</label>
          <input
            type="text"
            formControlName="agreementId"
            [(ngModel)]="agreementId"
            class="form-control"
            [ngClass]="{ 'is-invalid': submitted && contactFormf.agreementId.errors }"
          />
          <div *ngIf="submitted && contactFormf.agreementId.errors" class="invalid-feedback">
            <div *ngIf="contactFormf.agreementId.errors.required">Agreement Id is required</div>
          </div>
        </div>
        <div class="form-group">
          <label>Contact Type</label>
          <mat-select
          formControlName="contactType"
          [(ngModel)]="contactType"
          class="form-control">
          <mat-option *ngFor="let obj of contactTypes" [value]="obj.value"> {{ obj.viewValue }}</mat-option>
          </mat-select>
        </div>
        <div class="form-group">
          <label>Contact Type Description</label>
          <input type="text"
          formControlName="contactTypeDescription"
          [(ngModel)]="contactTypeDescription"
          class="form-control" />
        </div>
        <div class="form-group">
          <label>Contact Sub Type</label>
          <mat-select
          formControlName="contactSubType"
          [(ngModel)]="contactSubType"
          class="form-control">
          <mat-option *ngFor="let obj of contactTypes" [value]="obj.value"> {{ obj.viewValue }}</mat-option>
          </mat-select>
        </div>
        <div class="form-group">
          <label>Contact Sub Type Description</label>
          <input type="text"
          formControlName="contactSubTypeDescription"
          [(ngModel)]="contactSubTypeDescription"
          class="form-control" />
        </div>
        <div class="form-group">
          <label>Reference Number</label>
          <input type="text"
          formControlName="referenceNumber"
          [(ngModel)]="referenceNumber"
          class="form-control" />
        </div>
        <div class="form-group">
          <input
            matInput
            [min]="minDate"
            [max]="maxDate"
            [matDatepicker]="contactLastVerifiedDatepicker"
            formControlName="contactlastVerifiedDate"
            [(ngModel)]="contactlastVerifiedDate"
            placeholder="Choose Start date"
          />
          <mat-datepicker-toggle matSuffix [for]="contactLastVerifiedDatepicker"></mat-datepicker-toggle>
          <mat-datepicker #complaintStartDatepicker></mat-datepicker>
        </div>

        <div class="form-group">
          <input
            matInput
            [min]="minDate"
            [max]="maxDate"
            [matDatepicker]="contactStartDatepicker"
            formControlName="contactStartDate"
            [(ngModel)]="contactStartDate"
            placeholder="Choose Contact Start date"
          />
          <mat-datepicker-toggle matSuffix [for]="contactStartDatepicker"></mat-datepicker-toggle>
          <mat-datepicker #contactStartDate></mat-datepicker>
        </div>
        <div class="form-group">
          <input
            matInput
            [min]="minDate"
            [max]="maxDate"
            [matDatepicker]="contactEndDatepicker"
            formControlName="contactEndDate"
            [(ngModel)]="contactEndDate"
            placeholder="Choose Contact Start date"
          />
          <mat-datepicker-toggle matSuffix [for]="contactEndDatepicker"></mat-datepicker-toggle>
          <mat-datepicker #contactStartDate></mat-datepicker>
        </div>
      </form>
    </div>
  </div>
</div>

打字稿代码:

import { Component, OnInit, Output, EventEmitter } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
@Component({
selector: 'app-contactform',
templateUrl: './contactform.component.html',
styleUrls: ['./contactform.component.scss']
})
export class ContactformComponent implements OnInit {
contactForm: FormGroup;
contactId = '';
agreementId = '';
contactType = '';
contactTypeDescription = '';
contactSubType = '';
contactSubTypeDescription = '';
referenceNumber = '';
contactlastVerifiedDate = '';
contactStartDate = '';
contactEndDate = '';
contactTypes: any[] = [{ value: '', viewValue: '' }, { value: '', 
viewValue: '' }, { value: '', viewValue: '' }];
contactSubTypes: any[] = [{ value: '', viewValue: '' }, { value: '', 
viewValue: '' }, { value: '', viewValue: '' }];
constructor(private formBuilder: FormBuilder) {}
@Output()
savedContact: EventEmitter<any> = new EventEmitter<any>();
ngOnInit() {
this.contactForm = this.formBuilder.group({
  contactId: ['', Validators.required],
  agreementId: ['', Validators.required],
  contactType: ['', Validators.required],
  contactTypeDescription: [''],
  contactSubType: ['', Validators.required],
  contactSubTypeDescription: [''],
  referenceNumber: [''],
  contactlastVerifiedDate: ['', Validators.required],
  contactStartDate: ['', Validators.required],
  contactEndDate: ['']
 });
 }
 get contactFormf() {
 return this.contactForm.controls;
 }

 onSubmitcontactForm() {
// Stop here if Agent Relationship Form is invalid
if (this.contactForm.invalid) {
  return;
}
alert('SUCCESS!! :-)\n\n' + JSON.stringify(this.contactForm.value));
}
saveContact() {
var savedContact = {
  contactId: this.contactId,
  agreementId: this.agreementId,
  contactType: this.contactType,
  contactDescription: this.contactTypeDescription,
  contactSubType: this.contactSubType,
  contactSubTypeDescription: this.contactSubTypeDescription,
  referenceNumber: this.referenceNumber,
  lastVerifiedDate: this.contactlastVerifiedDate,
  startDate: this.contactStartDate,
  endDate: this.contactEndDate
  };
  this.savedContact.emit(savedContact);
  }
  }
  class Contact {
  contactId?: number;
  agreementId?: string[];
  contactType?: string;
  contactDescription?: string;
  endDate?: string;
  lastVerifiedDate?: string;
  referenceNumber?: string;
  startDate?: string;
  contactSubType?: string;
  contactSubTypeDescription?: string;
  }

通过阅读其他答案,我了解到我的 ngModel 声明和打字稿中的变量不匹配或类似的东西。但我不完全确定我哪里出错了。我为我的其他组件做了类似的事情,它们似乎工作正常。

【问题讨论】:

  • 您的contactStartDatepicker 和contactEndDatepicker 定义错误。

标签: html angular typescript material-ui


【解决方案1】:

这里正在使用 stackblitz example

你的contactStartDatepicker和contactEndDatepicker定义错误,我编辑如下,

<mat-form-field>
  <input matInput [matDatepicker]="startDate" placeholder="Choose a date">
  <mat-datepicker-toggle matSuffix [for]="startDate"></mat-datepicker-toggle>
  <mat-datepicker #startDate></mat-datepicker>
</mat-form-field>

<mat-form-field>
  <input matInput [matDatepicker]="endDate" placeholder="Choose a date">
  <mat-datepicker-toggle matSuffix [for]="endDate"></mat-datepicker-toggle>
  <mat-datepicker #endDate></mat-datepicker>
</mat-form-field>

【讨论】:

    【解决方案2】:

    您的mat-datepicker 引用变量mat-datepicker-toggle [for] 属性和[matDatepicker] 属性不匹配

    改成下面这样

            <div class="form-group">
              <input
                matInput
                [min]="minDate"
                [max]="maxDate"
                [matDatepicker]="contactLastVerifiedDatepicker"
                formControlName="contactlastVerifiedDate"
                [(ngModel)]="contactlastVerifiedDate"
                placeholder="Choose Start date"
              />
              <mat-datepicker-toggle matSuffix [for]="contactLastVerifiedDatepicker"></mat-datepicker-toggle>
              <mat-datepicker #contactLastVerifiedDatepicker></mat-datepicker>
            </div>
    
            <div class="form-group">
              <input
                matInput
                [min]="minDate"
                [max]="maxDate"
                [matDatepicker]="contactStartDatepicker"
                formControlName="contactStartDate"
                [(ngModel)]="contactStartDate"
                placeholder="Choose Contact Start date"
              />
              <mat-datepicker-toggle matSuffix [for]="contactStartDatepicker"></mat-datepicker-toggle>
              <mat-datepicker #contactStartDatepicker></mat-datepicker>
            </div>
            <div class="form-group">
              <input
                matInput
                [min]="minDate"
                [max]="maxDate"
                [matDatepicker]="contactEndDatepicker"
                formControlName="contactEndDate"
                [(ngModel)]="contactEndDate"
                placeholder="Choose Contact Start date"
              />
              <mat-datepicker-toggle matSuffix [for]="contactEndDatepicker"></mat-datepicker-toggle>
              <mat-datepicker #contactEndDatepicker></mat-datepicker>
            </div>
    

    【讨论】:

      猜你喜欢
      • 2019-01-07
      • 1970-01-01
      • 1970-01-01
      • 2018-08-27
      • 2022-12-19
      • 2017-07-15
      • 2017-06-26
      • 1970-01-01
      • 2017-11-14
      相关资源
      最近更新 更多