<!DOCTYPE html> <html ng-app="myApp"> <head> <meta charset="UTF-8"> <title>Title</title> <script src="angular-1.3.0.js"></script> <script type="text/javascript"> var rand = function () { return Math.ceil(Math.random()* 6); }; var app =angular.module('myApp',[]); app.controller('myCtrl',function ($scope,$http) { }); var rollDice =function () { var appElement= document.querySelector("[ng-controller=myCtrl]"); var $scope =angular.element(appElement).scope(); $scope.zhangsan=rand(); $scope.lisi=rand(); $scope.winer=($scope.zhangsan>$scope.lisi ? "张三":($scope.zhangsan<$scope.lisi ?"李四":"相等")) $scope.$apply(); // 同步到Angular控制器中 }; </script> </head> <body> <div ng-controller="myCtrl"> <div>张三:<input ng-model="zhangsan"></div> <div>李四:<input ng-model="lisi"></div> <div>赢方:<input ng-model="winer"></div> <div style="margin-left: 200px"><input type="button" value="掷骰子" οnclick="return rollDice();"/></div> </div> </body> </html>
掷骰子
最新推荐文章于 2020-05-20 14:03:30 发布