在ngModel

时间:2019-02-01 00:41:27

标签: angular ionic4 ngmodel

我将类导出到模块中并进行初始化,但无法在ngModel上使用它。我以前曾经可以使用它,但现在不能使用,而且我不知道这是什么错误,因为不向我显示任何错误消息。帮助:c 我敢肯定这很简单,但是我对Ionic的Angular还是陌生的。

我正在尝试制作秒表,并且在课堂上我有间隔,这会增加时间。我使用该变量在上使用ngModel在前端上显示时间(我正在研究Ionic 4)。我之前曾尝试过,但效果很好,但是后来我尝试添加一个服务,以从构造函数初始化类,然后把它弄乱了。我已经尝试删除所有项目,然后从Github上的最后一次提交重新开始(昨天是一切正常),然后复制/粘贴应该起作用但无效的代码。

这是在前端[home.page.html]:

  <ion-item class="container-stopwatch" lines="none">
    <ion-title>
      <ion-datetime #timer [ngModel]="this.timer.TIME" text-center readonly=true displayFormat="mm:ss"></ion-datetime>
    </ion-title>
    <ion-button (click)="reset()" *ngIf="show_reset" text-lowercase size="small" slot="end" color="light"><p style="color:red;">reset</p></ion-button>
  </ion-item>

这是该页面[home.page.ts]的文件.ts的一部分:

import { Timer } from './timer';
@Component({ 
    selector: 'app-home',
    templateUrl: 'home.page.html',
    styleUrls: ['home.page.scss'],
})
export class HomePage {

  constructor(public toastController: ToastController) {
    console.log(this.timer.TIME); // print '00:00:00.000'
  }

  public timer: Timer = new Timer();
  public list_times: string[] = []

  play() {
    console.log(this.timer);
    this.timer.run();
  }
}

最后,这是Timer [timer.ts]类的一部分:

export class Timer{
   constructor(){
      this.reset();
      this.isStart = false;
   }

   // Time of the timer
   public TIME:string = "00:00:00.000";

enter image description here

在控制台上,在home.page.ts构造函数上打印this.timer.TIME('00:00:00.000')的值,但是如果该类在该文件上正确初始化,为什么我不能使用相同的值前端变量?

编辑:

我在这里重复这个问题:

https://stackblitz.com/edit/ionic-nwahpb

1 个答案:

答案 0 :(得分:0)

HomePage是您的组件吗?如果是,我认为您缺少@component()装饰器。此外,我认为[ngModel]="this.timer.TIME"应该是[ngModel]="timer.TIME"。检查您的app.module.ts也是一个好主意。您要声明组件吗?

让我知道它是否有效。

相关问题