【问题标题】:How to bind data dynamically into leftTime in ngx-countdown如何在 ngx-countdown 中将数据动态绑定到 leftTime
【发布时间】:2020-09-21 06:05:13
【问题描述】:

我正在使用 ngx-countdown npm 包在我的网页上显示倒计时。 我想根据来自后端的数据设置leftTime属性,我尝试了以下方式,但它不起作用,

在我的 html 中,

<countdown #cd [config]="notifyConfig" (event)="handleEvent($event)"></countdown>

在我的 component.ts 中

notifyConfig: CountdownConfig = { leftTime: this.consultationDurationInSeconds, notify: [120], format: 'mm:ss', };

this.consultationDurationInSeconds = data.ConsultationDuration.Minutes*60;

我的数据对象有我想绑定到 leftTime 属性的分钟。但是当我尝试这种方式时,它会显示一条错误消息,

** 无法将“无效日期”转换为日期**,

谁能帮我解决这个问题?

【问题讨论】:

    标签: typescript countdown angular9


    【解决方案1】:

    我找到了解决办法,

    我在下面的项目中声明了类的顶部,

    consultationDurationInSeconds: any;

    notifyConfig = { leftTime: this.consultationDurationInSeconds, notify: [120], format: 'mm:ss' };

    在我获取数据的函数中,

    this.consultationDuration = data.ConsultationDuration.Minutes * 60;
    
    this.notifyConfig = { leftTime: this.consultationDuration, notify: [120], format: 'mm:ss' };
    
    

    【讨论】:

      【解决方案2】:

      你可以这样做

      首先声明一个包含你想要的时间的字符串,

      let TimeString = "20:00";
      
        startCountDown() {
          if (this.timer > 0) {
            this.TimeString = this.getFormattedTime();
            this.timer--;
          }
        }
      
        getFormattedTime() {
          const duration = moment.duration(this.timer, 'seconds');
          const resultstring = moment.utc(duration.asMilliseconds()).format('mm:ss');
      
          return resultstring;
        }
      

      你只想调用 startCountDown() 函数。

      【讨论】:

        猜你喜欢
        • 2019-03-01
        • 1970-01-01
        • 2017-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多