I am trying to learn AngularJS as a start to my revival of learning, I think that I am getting the hang of it so far, but would like to see what I can do differently with some super simple applications, hopefully I can see what I need to work on this way.
First thing is first, a basic calculator.
This is a super basic calculator, I haven't done anything super difficult yet. I did create 2 tables that each do something different, and only show when appropriate, one is a multiplication table and the other is a squares table.
other than that everything does what it should do so far.
var calculatorApp = angular.module("calculatorApp", []);
calculatorApp.factory('MathService', function() {
var factory = {};
factory.multiply = function(a, b) {
return a * b;
}
factory.add = function(a, b) {
return a + b;
}
factory.subtract = function(a, b) {
return a - b;
}
factory.divide = function(a, b) {
return a / b;
}
return factory;
});
calculatorApp.service('calcService', function(MathService) {
this.square = function(a) {
return MathService.multiply(a, a);
}
this.add = function(a, b) {
return MathService.add(a, b);
}
this.subtract = function(a, b) {
return MathService.subtract(a, b);
}
this.divide = function(a, b) {
return MathService.divide(a, b);
}
this.multiply = function(a, b) {
return MathService.multiply(a, b);
}
});
calculatorApp.controller('calcController', function($scope, calcService) {
$scope.squared = function() {
$scope.result = $scope.square($scope.number);
$scope.showSquares = true;
$scope.showMultiplicationTable = false;
}
$scope.add = function() {
$scope.output = calcService.add($scope.operand1, $scope.operand2);
$scope.showMultiplicationTable = false;
$scope.showSquares = false;
}
$scope.subtract = function() {
this.output = calcService.subtract(this.operand1, this.operand2);
$scope.showMultiplicationTable = false;
$scope.showSquares = false;
}
$scope.divide = function() {
this.output = calcService.divide(this.operand1, this.operand2);
$scope.showMultiplicationTable = false;
$scope.showSquares = false;
}
$scope.multiply = function() {
this.output = calcService.multiply(this.operand1, this.operand2);
$scope.showMultiplicationTable = true;
$scope.showSquares = false;
}
$scope.square = function(i) {
return calcService.square(i);
}
var arrayLength = 25;
var k = new Array(arrayLength);
for (var i = 0; i < arrayLength; i++) {
k[i] = i;
}
$scope.k = k;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="calculatorApp">
<h1>AngularJS - Calculator</h1>
<div ng-controller="calcController as calulator">
<label>Enter a number:</label>
<input type="number" ng-model="number" />
<button ng-click="squared()">X<sup>2</sup>
</button>
<p>Result: {{result}}</p>
<br />
<br />
<label>Enter a number:</label>
<input type="number" ng-model="operand1" />
<br />
<label>Enter a second number:</label>
<input type="number" ng-model="operand2" />
<br />
<button ng-click="add()">Add</button>
<button ng-click="subtract()">Subtract</button>
<button ng-click="multiply()">Multiply</button>
<button ng-click="divide()">Divide</button>
<p>Result: {{ output }}</p>
<table ng-show="showMultiplicationTable">
<tr>
<th ng-repeat="item in k">{{item+1}}</th>
</tr>
<tr ng-repeat="item in k">
<td ng-repeat="l in k">
{{(l + 1) * (item + 1)}}
</td>
</tr>
</table>
<table ng-show="showSquares">
<tr>
<th>intial</th>
<th>square</th>
</tr>
<tr ng-repeat="i in k">
<td>{{ i + 1 }}</td>
<td>{{square(i + 1)}}</td>
</tr>
</table>
</div>
</body>