Angular $ interval返回f不是函数

时间:2017-11-01 10:58:00

标签: javascript angularjs

我正在尝试以角度构建文本时钟,并且控制台返回错误“f不是函数”。有什么我想念的吗?

"use strict";
app.controller("dashboardController", ["$scope", "$location", "$interval", function ($scope, $location, $interval) {
    var dashboard = this;

    // Live Date and Time
    dashboard.currentTime = function () {
        var showMilitaryTime = false;
        var now = new Date;

        function showHours(theHour)
        {
            if (showMilitaryTime || (theHour > 0 && theHour < 13))
                return (theHour);
            else if (theHour == 0)
                return 12;
            else return (theHour - 12)
        }

        function fillZeros(inValue)
        {
            if (inValue > 9)
                return ":" + inValue;
            else return ":0" + inValue;
        }

        function showAmPm()
        {
            if (showMilitaryTime)
                return ("");
            if (now.getHours() < 12)
                return (" AM");
            else return (" PM");
        }

        function showTime(elm)
        {
            var theTime = showHours(now.getHours()) + fillZeros(now.getMinutes()) + fillZeros(now.getSeconds()) + showAmPm();
            document.getElementById(elm).innerHTML = theTime;
            $interval("showTime('"+elm+"')", 1000);
        }

        showTime('clock'); 
    }

    // Initialise functions in the dashboard
    dashboard.init = function () {
        dashboard.currentTime();
    };

    dashboard.init();
}]);

1 个答案:

答案 0 :(得分:3)

您需要将回调放在那里,而不是代码为string

$interval(function() {
    showTime(elm);
}, 1000);

https://docs.angularjs.org/api/ng/service/ $间隔#使用