AngularJS实现的生成随机数与猜数字大小功能示例

花园里的花真多啊,多的就像天上的星星,密密麻麻;花园里的花真彩啊,一枝枝花朵五彩斑斓,向天边的霞光,那么耀眼,像高空的彩虹那么绚烂;花园里的花真香啊,香的让你感觉到自己仿佛在画中游玩一样,这时有人拍我一下,我才知道自己在花园里游玩。

本文实例讲述了AngularJS实现的生成随机数与猜数字大小功能。分享给大家供大家参考,具体如下:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>www.haodaima.com Angular猜数</title>
 <style>
  *{
   margin: 0;
   padding: 0;
   font-size: 30px;
  }
  input{
   width: 500px;
   height: 50px;
   font-size: 30px;
  }
  button{
   width: 80px;
   height: 50px;
   border: 0;
   text-align: center;
   line-height: 50px;
   background: darkblue;
   color: #fff;
   margin-left: 5px;
  }
 </style>
 <script src="angular/angular.js"></script>
 <script>
  var myapp=angular.module("myapp",[]);
  myapp.controller("myCtrl",function ($scope) {
    $scope.check=function () {
     console.log($scope.random);
     $scope.differ=$scope.guess-$scope.random;
     $scope.num++;
    };
    $scope.reset=function () {
     $scope.differ=null;
     $scope.guess=null;
     $scope.num=0;
     $scope.random=Math.ceil(Math.random()*10);
    };
    $scope.reset();
  })
 </script>
</head>
<body ng-app="myapp" ng-controller="myCtrl">
<h2>请输入一个1-10的整数</h2>
<input type="text" ng-model="guess"><button ng-click="check()">检查</button><button ng-click="reset()">重置</button>
<p ng-if="differ>0">猜大了</p>
<p ng-if="differ<0">猜小了</p>
<p ng-if="differ==0">猜对了</p>
<p>一共猜了<span ng-bind="num"></span>次</p>
</body>
</html>

运行效果:

PS:这里再为大家提供几款相关在线工具供大家参考:

在线随机数字/字符串生成工具:
http://tools.haodaima.com/aideddesign/suijishu

在线随机字符/随机密码生成工具:
http://tools.haodaima.com/aideddesign/rnd_password

高强度密码生成器:
http://tools.haodaima.com/password/CreateStrongPassword

希望本文所述对大家AngularJS程序设计有所帮助。

本文AngularJS实现的生成随机数与猜数字大小功能示例到此结束。人必须要受过伤才会沉默专注,无论是心灵或肉体上的创伤,对成长都有益处。小编再次感谢大家对我们的支持!

您可能有感兴趣的文章
AngularJS tab栏实现和mvc小案例实例详解

基于AngularJS拖拽插件ngDraggable.js实现拖拽排序功能

AngularJS实现的自定义过滤器简单示例

ionic使用angularjs表单验证(模板验证)

用WebStorm进行Angularjs 2开发(环境篇:Windows 10,Angular-cli方式)