I'm using AngularJS to make a simple calculator, and for the life of me, I cannot figure out how to make it work. My assignment was to adapt a java calculator program to javascript, then give it a view. Here's the HTML file I'm using:
<!DOCTYPE html>
<html ng-app>
<head>
<title>Calculator</title>
<script src="js/angular.min.js"></script>
<script src="js/calculator.js"></script>
<script>
function CalcController($scope){
$scope.calc = Calculator;
}
</script>
</head>
<body ng-controller='CalcController'>
<p>{{calc.val}}</p>
</body>
</html>
The file "calculator.js" creates an object called Calculator. I'm trying to use the methods and variables from that file in my HTML file, but {{calc.val}} doesn't display correctly.
I'm probably doing a ton of things horribly wrong, but can someone help?
Here's the calculator.js file:
var Calculator = {
val: 0;
old: 0;
op: '=';
isClean: true;
process: function(c){
if(isClear(c)){
this.val = 0;
this.old = 0;
this.op = '=';
this.isClean = true;
}
else if(isDigit(c)){
var d = evalDigit(c);
if(this.isClean){
this.old = this.val;
this.val = d;
}
else{
this.val = (this.val * 10) + d;
}
this.isClean = false;
}
else if(isOp(c)){
var v = evalOp(this.op, this.old, this.val){
if(!this.isClean){
this.old = this.val;
}
this.val = v;
this.op = c;
this.isClean = true;
}
}
}
isOp: function(c){
switch(c){
case '=' : return true;
case '+' : return true;
case '-' : return true;
case '*' : return true;
}
return false;
}
evalOp: function(c, m, n){
switch(c){
case '=' : return n;
case '+' : return m + n;
case '-' : return m - n;
case '*' : return m * n;
}
}
isDigit: function(c){
return c >= '0' && c <= '9';
}
evalDigit: function(c){
return c - '0';
}
isClear: function(c){
return c == 'c';
}
};
Also, I'm using AngularJS 1.2.
calculator.js?