Joey Zhang
Joey Zhang

Reputation: 363

How can I call a function in an angular controller?

I have a angularJS file like this; I need to call the newGame function in the end of the controller not in html. Can anyone help me with this?

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!"
    }
};     
});

Upvotes: 0

Views: 45

Answers (1)

John Smith
John Smith

Reputation: 2341

It's a function just like any other function. Add $scope.newGame(); at the end of your controller.

Upvotes: 1

Related Questions