角度2计数计时器

时间:2017-07-12 07:54:38

标签: angular typescript

有人可以在角度给我计数计时器示例并提前感谢

我将这个TypeScript代码添加到我的项目时没有任何反应

var minutesLabel = document.getElementById("minutes");
var secondsLabel = document.getElementById("seconds");
var totalSeconds = 0;
setInterval(setTime, 1000);

function setTime() {
    ++totalSeconds;
    secondsLabel.innerHTML = pad(totalSeconds%60);
    minutesLabel.innerHTML = pad(parseInt(totalSeconds/60));
}

function pad(val) {
    var valString = val + "";
    if(valString.length < 2) {
        return "0" + valString;
    } else {
        return valString;
    }
}

2 个答案:

答案 0 :(得分:1)

You can use this directive provided by "siddii" https://siddii.github.io/angular-timer/

答案 1 :(得分:0)

This is probably because you aren't doing it the Angular2 way. You are currently modifying the DOM directly using the document property. Yet this is a bad habit and you should do this using the build in Components and Viewbindings Angular2 supports. Instead of doing stuff like secondsLabel.innerHTML = pad(totalSeconds%60);, just use properties that you bind to your view, then it should work.