lundi 5 décembre 2016

Alert box if condition matches in angular js

I am writing a simple angular js application. The application asks answer for multiplication of 2 numbers

enter image description here

I want that when button 'Check' is clicked, the function should check if the answer is correct. If answer in wrong, only then it should display alert message.

How can I do this using angular js?

  var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $window) {
  $scope.firstname = "John";
  $scope.showTable = false;
  $scope.changeName = function() {
    $scope.firstname = $scope.table;
  }

  $scope.generateRandomNumber = function() {
    $scope.Rn = Math.floor(Math.random() * 31);
    $scope.showTable = true;
  }

  $checkAnswer = function() {
    //if ($scope.Rn * $scope.table == $scope.answer)
    $window.alert("Right!");
    // return;
  }

  $scope.randomNumber = function() {
    $scope.firstname = Math.floor(Math.random() * 31);
  }
});
<script src="http://ift.tt/1mQ03rn"></script>
<!DOCTYPE html>
<html>

<body>
  <div ng-app="myApp" ng-controller="myCtrl">
    <label>
      Enter the table you want to practice :
    </label>
    <input type="text" ng-model="table" />
    <button ng-click="generateRandomNumber()">
      Start</button>
    <h1 ng-show="showTable">
             *  =
            <input type="text" ng-model="answer" />
            <button ng-click="checkAnswer()">
                Check</button></h1>

  </div>

</body>

</html>

Aucun commentaire:

Enregistrer un commentaire