2015-05-18 51 views
1

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

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();} 
+0

http://www.tutorialspoint.com/javascript/math_sqrt.htm你能不能做这样的事情? –

+0

虽然有可能将数学注入与Angular一起使用,但它并不是处理该问题的角度方式。在Angular中,你会创建自己的平方根函数(使用牛顿方法将是最好的) – snaplemouton

回答

0

您需要提供您希望从到Math.sqrt()功能提取平方根作为argume数nt如:

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

Math.sqrt是一个函数,这意味着你需要把数您希望将功能应用到括号内,像这样

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