搜索
您的当前位置:首页正文

angularjs实现猜数字大小功能

2020-11-27 来源:步旅网

本文实例为大家分享了angularjs实现猜数字大小功能的具体代码,供大家参考,具体内容如下

<body ng-app="myapp" ng-controller="myCtrl">
 <h2>猜一猜,多大值?(1-1000)</h2>
 我猜是:<input type="text" ng-model="guess">
 <button ng-click="check()">验证</button><button ng-click="reset()">再玩一次</button><br>
 <span ng-if="fil>0">您猜的数大了</span>
 <span ng-if="fil<0">您猜的数小了</span>
 <span ng-if="fil==0">您猜对了</span>
 <p>猜的次数<span>{{n}}</span></p>
</body>

js代码:

<script src="angular.min.js"></script>
 <script>
 var myapp=angular.module("myapp",[]);
 myapp.controller("myCtrl",function ($scope) {
      //验证
 $scope.check=function () {
 console.log($scope.random); //根据差值判断显示、隐藏状态
 $scope.fil=$scope.guess-$scope.random;
 $scope.n++;

 };          //重置方法
 $scope.reset=function () {
 $scope.guess=null;
 $scope.fil=null;
 $scope.n = 0;           //得到随机数
 $scope.random=Math.ceil(Math.random()*1000);
 };
 $scope.reset();
 })



 </script>
Top