Javascript 使用两个文本框的简单计算器,用于AngularJS中的square?

Javascript 使用两个文本框的简单计算器,用于AngularJS中的square?,javascript,angularjs,Javascript,Angularjs,这是我的密码: <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> </head> <body> <div ng-app="app"> <div ng-controller="CalculatorController"&g

这是我的密码:

    <html>

<head>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>

<body>
  <div ng-app="app">
    <div ng-controller="CalculatorController">
      Enter a number:
      <input type="number" ng-model="number" />
      <br /> Enter a number:
      <input type="number" ng-model="number2" />
      <br />
      <div>
        <button ng-click="doSquare()">X<sup>2</sup></button>
        <button ng-click="doCube()">X<sup>3</sup></button>
      </div>
      <br />
      <div>
        <button ng-click="doAdd()">Add</button>
        <button ng-click="doSubstract()">Substract</button>
        <button ng-click="doMultiply()">Multiply</button>
        <button ng-click="doDivide()">Divide</button>
        <button ng-click="doModulus()">Modulus</button>
      </div>
      <div>Answer: {{answer}}</div>
      <div>
        <button type="button" ng-click="clear('filter')">Clear</button>
      </div>
    </div>
  </div>
  <script>
    var app = angular.module('app', []);

    app.service('MathService', function() {
      this.add = function(a, b) {
        return a + b
      };

      this.substract = function(a, b) {
        return a - b
      };

      this.multiply = function(a, b) {
        return a * b
      };

      this.divide = function(a, b) {
        return a / b
      };

      this.modulus = function(a, b) {
        return a % b
      };
    });

    app.service('CalculatorService', function(MathService) {

      this.square = function(a) {
        return MathService.multiply(a, a);
      };
      this.cube = function(a) {
        return MathService.multiply(a, MathService.multiply(a, a));
      };
      this.add = function(a, b) {
        return MathService.add(a, b);
      };
      this.substract = function(a, b) {
        return MathService.substract(a, b);
      };
      this.multiply = function(a, b) {
        return MathService.multiply(a, b);
      };
      this.divide = function(a, b) {
        return MathService.divide(a, b);
      };
      this.modulus = function(a, b) {
        return MathService.modulus(a, b);
      };
    });

    app.controller('CalculatorController', function($scope, CalculatorService) {

      $scope.doSquare = function() {
        $scope.answer = CalculatorService.square($scope.number);
        $scope.answer = CalculatorService.square($scope.number2);
      }

      $scope.doCube = function() {
        $scope.answer = CalculatorService.cube($scope.number);
        $scope.answer = CalculatorService.cube($scope.number2);
      }
      $scope.doAdd = function() {
        $scope.answer = CalculatorService.add($scope.number, $scope.number2);
      }
      $scope.doSubstract = function() {
        $scope.answer = CalculatorService.substract($scope.number, $scope.number2);
      }
      $scope.doMultiply = function() {
        $scope.answer = CalculatorService.multiply($scope.number, $scope.number2);
      }
      $scope.doDivide = function() {
        $scope.answer = CalculatorService.divide($scope.number, $scope.number2);
      }
      $scope.doModulus = function() {
        $scope.answer = CalculatorService.modulus($scope.number, $scope.number2);
      }
      $scope.clear = function(answer) {
       $scope.answer = null; 
      }
    });
  </script>
</body>

</html>

输入一个数字:

输入一个数字:
X2 X3
添加 减去 乘 分 模数 答复:{{答复} 清楚的 var-app=angular.module('app',[]); app.service('MathService',function(){ this.add=函数(a,b){ 返回a+b }; this.substract=函数(a,b){ 返回a-b }; this.multiply=函数(a,b){ 返回a*b }; this.divide=函数(a,b){ 返回a/b }; 该系数=函数(a,b){ 返回%b }; }); 应用程序服务('CalculatorService',函数(MathService){ this.square=函数(a){ 返回MathService.multiply(a,a); }; this.cube=函数(a){ 返回MathService.multiply(a,MathService.multiply(a,a)); }; this.add=函数(a,b){ 返回MathService。添加(a,b); }; this.substract=函数(a,b){ 返回MathService.substract(a,b); }; this.multiply=函数(a,b){ 返回MathService.multiply(a,b); }; this.divide=函数(a,b){ 返回MathService.divide(a,b); }; 该系数=函数(a,b){ 返回MathService。模数(a,b); }; }); app.controller('CalculatorController',function($scope,CalculatorService){ $scope.doSquare=函数(){ $scope.answer=CalculatorService.square($scope.number); $scope.answer=CalculatorService.square($scope.number2); } $scope.doCube=函数(){ $scope.answer=CalculatorService.cube($scope.number); $scope.answer=CalculatorService.cube($scope.number2); } $scope.doAdd=函数(){ $scope.answer=CalculatorService.add($scope.number,$scope.number2); } $scope.doSubstract=函数(){ $scope.answer=CalculatorService.substract($scope.number,$scope.number2); } $scope.doMultiply=function(){ $scope.answer=CalculatorService.multiply($scope.number,$scope.number2); } $scope.doDivide=函数(){ $scope.answer=CalculatorService.divide($scope.number,$scope.number2); } $scope.doModulus=function(){ $scope.answer=CalculatorService.module($scope.number,$scope.number2); } $scope.clear=函数(答案){ $scope.answer=null; } });
当我输入第一个文本框并单击X^2时,我应该得到答案;当我将输入第二个文本框并单击X^2时,我应该得到答案。 怎么做? 有人能帮我吗? 以下是plunker链接:

当我输入第一个文本框并单击X^2时,我应该得到答案;当我将输入第二个文本框并单击X^2时,我应该得到答案。怎么做

您可以使用
| |
(或)运算符

$scope.doSquare = function() {
    $scope.answer = CalculatorService.square($scope.number) || 
                    CalculatorService.square($scope.number2);
}
这可能不是最好的方式,我也可能不完全相信它应该如何运作,但这会解决你的问题


请注意,使用此代码,如果第一个文本框的值存在,则第二个文本框将不会出现

您必须在x^2函数中包含if条件,以检查哪个文本框是填充的,并执行它。.旁注:在调用服务之前,将
$scope.number
$scope.number2
解析为numbermethods@neo.abi1000最简单的方法是得到两个答案,并在两个值都存在时显示这两个答案。。应该很容易为您实现