如何在角度控制器中调用函数?

时间:2016-05-20 16:04:21

标签: javascript angularjs

我有一个像这样的angularJS文件;我需要在控制器的末尾调用newGame函数而不是html。任何人都可以帮我这个吗?

var app = angular.module("myApp", []);

app.controller("myCtrl", function ($scope) {

$scope.newGame = function () {
    $scope.random = Math.floor(Math.random() * 100) + 1;
    $scope.display = "Start guessing";
};

$scope.giveUp = function () {
    $scope.display = $scope.random;
};

$scope.check = function () {
    if ($scope.guess > $scope.random) {
        $scope.display = "Guess Higher"
    } else if ($scope.guess < $scope.random) {
        $scope.display = "Guess Lower"
    } else {
        $scope.display = "You got it!"
    }
};     
});

1 个答案:

答案 0 :(得分:1)

它就像任何其他功能一样。在控制器末尾添加$scope.newGame();