【问题标题】:AngularJS square rootAngularJS 平方根
【发布时间】:2015-05-18 19:57:07
【问题描述】:

我正在使用 HTML + CSS + AngularJS 制作一个简单的计算器。一切正常,但我想添加一个 SquareRoot 函数。 这是一些代码:

 function _solve(){
switch(_state) {
  case 'ADD':
    _total +=  parseFloat($scope.console);
    $scope.console = 0;
    break; //similar for the rest of the operations

$scope.add = function() {
    _solve();
_state = 'ADD';} //this is how i call the function.

这是我对 Sqrt 的想法,但我认为不好:

$scope.getSqrt = function() {
$scope.console = (parseFloat($scope.console) * Math.Sqrt).toString();}

【问题讨论】:

  • tutorialspoint.com/javascript/math_sqrt.htm这样的事情你能不做吗?
  • 虽然可以注入 Math 以与 Angular 一起使用,但这不是 Angular 处理问题的方式。在 Angular 中,您将创建自己的平方根函数(最好使用牛顿法)

标签: html css angularjs square-root


【解决方案1】:

您需要将希望从中提取平方根的数字作为参数提供给Math.sqrt() 函数,例如:

Math.sqrt(parseFloat($scope.console).toString();

【讨论】:

    【解决方案2】:

    Math.sqrt 是一个函数,这意味着您需要将希望应用该函数的数字放在括号内,就像这样

    Math.sqrt(parseFloat($scope.console)).toString();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-13
      • 1970-01-01
      相关资源
      最近更新 更多