gpt4 book ai didi

javascript - 使用两个文本框用于 AngularJS 中的正方形的简单计算器?

转载 作者:行者123 更新时间:2023-12-03 04:19:54 25 4
gpt4 key购买 nike

这是我的代码:

    <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>

当我向第一个文本框提供输入并单击 X^2 时,我应该得到答案,当我向第二个文本框提供输入然后单击 X^2 时,我应该得到答案。怎么做?谁能帮我吗?这是骗子链接:http://plnkr.co/edit/8Y47MDICWdeBTiJzaUnP?p=preview

最佳答案

When I give input to first textbox and by clicking X^2 i should get the answer and when I will give the input to 2nd textbox and then also by clicking the X^2 I should get the answer. How to do that?

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

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

这可能不是最好的方法,我可能不完全相信它应该如何表现,但这可以解决您的问题。

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

关于javascript - 使用两个文本框用于 AngularJS 中的正方形的简单计算器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43993382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com