错误:ExpressionChangedAfterItHasBeenCheckedError

时间:2018-03-17 19:16:54

标签: ionic-framework

我正在尝试使用ion-input上的值设置attendance.studentName的值。但是我收到了一个错误。

我想将离子输入和离子选择的值推到firebase。

  <!-- When I delete the ngModel it works -->
  <ion-input disabled="true" value="{{student.name}}" [(ngModel)]="attendance.studentName" > {{student.name}} </ion-input>

  <ion-select item-end [ngModel]="attendance.status">
    <ion-option value="Present">Present</ion-option>
    <ion-option value="Absent">Absent</ion-option>
    <ion-option value="Late">Late</ion-option>
  </ion-select>
</ion-item>

**编辑**

我从firebase获取输入的值,我不希望用户编辑它,所以我禁用了它。然后我使用ngModel获取输入。

这是我的代码..

  

TS

import { Student } from '../../models/students/students.model';
import { Attendance } from '../../models/attendance/attendance.model';


export class CheckAttendancePage {

  studentList$: Observable<Student[]>

  attendance: Attendance = {
    studentName: '',
    status: ''
  }

  constructor(private afDatabase: AngularFireDatabase, private studentsList: StudentListService, private attendanceList: AttendanceListService,
  public navCtrl: NavController, public navParams: NavParams) {
    this.studentList$ = this.studentsList
      .getStudentList()
      .snapshotChanges()
      .map(
        changes => {
          return changes.map(c => ({
          key: c.payload.key, ...c.payload.val()
          }))
        }
      )
  }

addAttendance(attendance: Attendance){
    console.log(attendance)
}
  

HTML

1 个答案:

答案 0 :(得分:0)

.ts文件中需要设置attendance对象

attendance:any={'studentName': '', .'status':''}

在构造函数中,您可以设置此值

this.attendance.studentName = your firbase name

.html档案

<ion-item>

  <ion-input disabled="true" [(ngModel)]="attendance.studentName" type="text">
  </ion-input>

  <ion-select item-end [(ngModel)]="attendance.status">
    <ion-option value="Present">Present</ion-option>
    <ion-option value="Absent">Absent</ion-option>
    <ion-option value="Late">Late</ion-option>
  </ion-select>
</ion-item>

在构造函数中设置attendance.studentName

constructor(private firebaseDB: AngularFireDatabase){

  this.firabase= this.firebaseDB.list('/users');// your route
  this.attendance.studentName= this.firabase.name;
}

在用户定义方法中设置attendance.studentName的值,假设其名称为Dbcall

首先在public navCtrl: NavController

中注入constructor
Dbcall(){
  this.attendance.studentName= database student value
  this.navCtrl.setRoot(this.navCtrl.getActive().component);
}