angularjs 实现猜数字大小的功能
1 <body ng-app="myapp" ng-controller="myCtrl"> 2 <h2>猜一猜,多大值?(1-1000)</h2> 3 我猜是:<input type="text" ng-model="guess"> 4 <button ng-click="check()">验证</button><button ng-click="reset()">再玩一次</button><br> 5 <span ng-if="fil>0">您猜的数大了</span> 6 <span ng-if="fil<0">您猜的数小了</span> 7 <span ng-if="fil==0">您猜对了</span> 8 <p>猜的次数<span>{{n}}</span></p> 9 </body>
1 <script src="angular.min.js"></script> 2 <script> 3 var myapp=angular.module("myapp",[]); 4 myapp.controller("myCtrl",function ($scope) { 5 //验证 6 $scope.check=function () { 7 console.log($scope.random);
//根据差值判断显示、隐藏状态 8 $scope.fil=$scope.guess-$scope.random; 9 $scope.n++; 10 11 };
//重置方法 12 $scope.reset=function () { 13 $scope.guess=null; 14 $scope.fil=null; 15 $scope.n = 0;
//得到随机数 16 $scope.random=Math.ceil(Math.random()*1000); 17 }; 18 $scope.reset(); 19 }) 20 21 22 23 </script>