控制器内部的角度方法调用

Angular method call inside controller

本文关键字:方法 调用 内部 控制器      更新时间:2023-09-26

这是 plunk githubviewer 我希望有人能帮忙。根据教程,我应该通过调用控制器来获得一个弹出窗口 countDownController 这是JS文件的片段

getstartedapp.controller('countDownController', function countDownController($scope, $interval, $window) {
    $scope.countdown = 5;
    startCountdown;
    var startCountdown = function () {
        $window.alert(1);
    };
    var decrementFunction = function () {
        $scope.countdown = -1
        if ($scope.countdown < 1) {
            $scope.search($scope.username);
        }
    };
});

这是它正在加载的 HTML

<div ng-controller="countDownController">
    {{ countdown }}
</div>

加载页面时。关于我可能错过什么的任何想法?谢谢。

你必须调用startCountdown()而不是startCountdown。在 plnkr 中,您还必须将该调用放在函数定义之后。

普罗提

getstartedapp.controller('countDownController', function countDownController($scope, $interval,$window) {
    $scope.countdown = 5;
    var startCountdown = function () {
        $window.alert(1);
    };
    var decrementFunction = function () {
        $scope.countdown = -1
        if ($scope.countdown < 1) {
            $scope.search($scope.username);
        }
    };
    startCountdown();
});