这是一个使用角度的简单计算器。 addScv没有作为服务获得。我如何解决这个问题?这里我只为添加部分添加了服务。
我的代码 -
var name="myApp";
requires=[];
app=angular.module(name,requires);
app.service("addSvc", function() {
this.add = function(firstNum, secondNum) {
return parseInt(firstNum) + parseInt(secondNum);
}
})
app.controller('calcCtrl',function($scope,addSvc){
console.log("calcCtrl works!");
var opp;
ans=0;
opp=$scope.singleSelect;
console.log(opp);
$scope.calculateNums=function(a,b){
console.log("function works!");
console.log($scope.n1);
console.log($scope.n2);
console.log($scope.singleSelect);
if($scope.singleSelect=="+"){
ans= addScv.add(a,b);
console.log(ans);
}else if ($scope.singleSelect=="-"){
ans= (parseInt(a) - parseInt(b));
}else if ($scope.singleSelect=="*"){
ans= (parseInt(a) * parseInt(b));
}else if ($scope.singleSelect=="/"){
ans= (parseInt(a) / parseInt(b));
}
console.log(ans);
return ans;
}
});
<html ng-app="myApp">
<head>
<script src="angular.js"></script>
<script src="app.js"></script>
</head>
<body>
<h3>Hello</h3>
<div ng-controller='calcCtrl' >
Number 1::<input Type="text" name="number1" ng-model="n1"/ required> <br/>
Number 2::<input Type="text" name="number2" ng-model="n2"/ required> <br/>
operator::<select ng-model="singleSelect">
<option value="+">+</option>
<option value="*" >*</option>
<option value="-">-</option>
<option value="/">/</option>
</select></p> <br/>
<button ng-click="calculateNums(n1,n2)">Calculate Now</button>
</div>
</body>
</html>
上面的代码标识了插入的值。 输出
calcCtrl works!
app.js:20 undefined
app.js:23 function works!
app.js:24 2
app.js:25 3
app.js:26 +
angular.js:9505 ReferenceError: addScv is not defined
at ChildScope.$scope.calculateNums (app.js:28)
at angular.js:10290
at angular.js:18784
at ChildScope.$eval (angular.js:12069)
at ChildScope.$apply (angular.js:12167)
at HTMLButtonElement.<anonymous> (angular.js:18783)
at angular.js:2671
at forEach (angular.js:329)
at HTMLButtonElement.eventHandler (angular.js:2670)
(anonymous) @ angular.js:9505
(anonymous) @ angular.js:6947
$apply @ angular.js:12169
(anonymous) @ angular.js:18783
(anonymous) @ angular.js:2671
forEach @ angular.js:329
eventHandler @ angular.js:2670
这里也没有给出答案。 没有上面的服务代码正常工作。 希望得到一个明确的解释。谢谢。