【问题标题】:Populate input using @Input inside Bootstrap 4 modal在 Bootstrap 4 模式中使用 @Input 填充输入
【发布时间】:2017-04-01 02:14:44
【问题描述】:

我有一个带有表格的主页,当点击一行时,它使用@Output 发送该行的数据(我已经通过在另一个地方使用它确认数据正在正确发送项目)。然后,当我单击左下方显示“数据点信息”的按钮时,会弹出一个 Bootstrap 4 模式。我需要做的是从单击的行中获取数据,并用它填充模态框内的表单。

主页:

模态:

模态的HTML:

<div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog bodyWidth">
<div class="modal-content wide">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h4 class="modal-title">Update Data Point</h4>
  </div>
  <div class="modal-body">
    <form class="form-inline" [formGroup]="updateForm" (ngSubmit)="submitForm(updateForm.value)">
      <div class="row">
        <div class="form-group" [ngClass]="{'has-error':!updateForm.controls['dataPoint'].valid && updateForm.controls['dataPoint'].touched}">
          <label>Data Point:</label>
          <input class="form-control special" type="text" [formControl]="updateForm.controls['dataPoint']">
        </div>
        <div class="form-group move" [ngClass]="{'has-error':!updateForm.controls['ICCP'].valid && updateForm.controls['ICCP'].touched}">
          <label >ICCP:</label>
          <input type="text" class="form-control special" [formControl]="updateForm.controls['ICCP']">
        </div>
        <div class="form-group" [ngClass]="{'has-error':!updateForm.controls['startDate'].valid && updateForm.controls['startDate'].touched}">
          <label>Start Date:</label>
          <input [value]="getDate('start')" class="form-control special" type="text" [formControl]="updateForm.controls['startDate']" style="margin-right: 4px;">
        </div>
        <div style="display:inline-block">
          <ngb-datepicker id="special" *ngIf="startCheck;" [(ngModel)]="startDate" (ngModelChange)="showDatePick(0)" [ngModelOptions]="{standalone: true}"></ngb-datepicker>
        </div>
        <button type="button" class="btn icon-calendar closest" (click)="showDatePick(0)"></button>
        <div class="form-group" [ngClass]="{'has-error':!updateForm.controls['endDate'].valid && updateForm.controls['endDate'].touched}">
          <label >End Date:</label>
          <input [value]="getDate('end')" class="form-control special" type="text" [formControl]="updateForm.controls['endDate']">
        </div>
        <div style="display:inline-block">
          <ngb-datepicker id="special" *ngIf="endCheck;" [(ngModel)]="endDate" (ngModelChange)="showDatePick(1)" [ngModelOptions]="{standalone: true}"></ngb-datepicker>
        </div>
        <button type="button" class="btn icon-calendar closer" (click)="showDatePick(1)"></button>
      </div>
    </form>
  </div>
  <div class="modal-footer">
    *All Fields Are Required. End Date must be after Start Date
    <button type="submit" class="btn" [disabled]="!updateForm.valid" data-dismiss="modal">Update</button>
    <button type="button" (click)="resetForm()" class="btn" data-dismiss="modal">Cancel</button>
  </div>
</div>
</div>
</div>

模态的打字稿:

@Component({
  selector: 'update-validation',
  styleUrls: ['../app.component.css'],
  templateUrl: 'update.component.html',
  providers: [DatePipe]
})
export class UpdateComponent {
  @Input() receivedRow:DataTable;
  public dt: NgbDateStruct;
  public dt2: NgbDateStruct;
  public startCheck: boolean = false;
  public endCheck: boolean = false;
  updateForm : FormGroup;

 constructor(fb: FormBuilder, private datePipe: DatePipe){
   this.updateForm = fb.group({
    'dataPoint' : [DPS[0].tDataPoint, Validators.required],
    'ICCP' : [DPS[0].tICCP, Validators.required],
    'startDate' : [DPS[0].tStartDate, Validators.required],
    'endDate' : [DPS[0].tEndDate, Validators.required]
 }, {validator: this.endDateAfterOrEqualValidator})
 }

resetForm(){
  location.reload();
  //this.updateForm.reset();
}

submitForm(value: any){
  console.log(value);
}

public getDate(dateName: string) {
  let workingDateName = dateName + 'Date';
  let timestamp = this[workingDateName] != null ? new Date(this[workingDateName].year, this[workingDateName].month-1, this[workingDateName].day).getTime() : new Date().getTime();
  this.updateForm.controls[dateName + 'Date'].setValue(this.datePipe.transform(timestamp, 'MM/dd/yyyy'));
}

public showDatePick(selector):void {
  if(selector === 0) {
    this.startCheck = !this.startCheck
  } else {
    this.endCheck = !this.endCheck;
  }
}

endDateAfterOrEqualValidator(formGroup): any {
  var startDateTimestamp, endDateTimestamp;
  for(var controlName in formGroup.controls) {
    if (controlName.indexOf("startDate") !== -1) {
      startDateTimestamp = Date.parse(formGroup.controls[controlName].value);
    }
    if (controlName.indexOf("endDate") !== -1) {
      endDateTimestamp = Date.parse(formGroup.controls[controlName].value);
    }
  }
  return (endDateTimestamp < startDateTimestamp) ? { endDateLessThanStartDate: true } : null;
  }
}

主页中的 HTML 使用它的选择器将模式放置在此处(toSend 的类型为 DataTable,这是我从主页的 Typescript 发送的行中的数据):

<update-validation [receivedRow]='toSend'></update-validation>

由于我使用的是 @Output 和 @Input,我不确定为什么我的 Typescript 中的 receivedRow 未定义。

【问题讨论】:

  • 你试过了吗[receivedRow]="'toSend'"(我假设 'toSend' 只是字符串)?
  • 是的,这不会改变任何事情。如果我尝试在我的 Typescript 中访问 receivedRow 的任何属性,则表示这些值仍未定义。

标签: html twitter-bootstrap angular typescript bootstrap-modal


【解决方案1】:

原因是当你的modal组件初始化时,没有receivedRow。你应该用*ngIf指令和ngOnChange这样的方法来控制它;

//xyz is just any field on your parent component

//in html
<div *ngIf="xyz">
     <update-validation [receivedRow]="xyz"></update-validation>
</div>

//in component of modal
ngOnChanges(){
     if(receivedRow){
          //do whatever you want
     }
}

【讨论】:

  • 现在我正确接收receivedRow,但如果我尝试在我的构造函数中使用它,如'dataPoint' : [this.receivedRow.tDataPoint, Validators.required],它仍然说tDataPoint 未定义并且不允许弹出模式.在我的 Typescript 的方法中打印 receivedRow 的各种属性是可行的,那么为什么我只能在构造函数中分配这些属性值?
  • 您还应该在组件收到receivedRow 时分配this.receivedRow.tDataPoint,因为当您分配dataPoint 时,receivedRow 还没有收到。我建议你这样做;首先,设置'dataPoint'为空; 'dataPoint': [null, ...],之后在ngOnChanges方法中更新'dataPoint'的值
  • 是的,updateForm.controls['dataPoint'].setValue(receivedRow.tDataPoint)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多