HTML:
<div ng-controller="MainCtrl as ctrl">
<input type="text" ng-model="ctrl.number">
<h2>{{ctrl.number}}</h2>
<button ng-click="ctrl.factorial(ctrl.number)">
Click
</button>
</div>
JS:
angular
.module('myApp', [])
.controller('MainCtrl', MainCtrl)
function MainCtrl() {
var ctrl = this;
ctrl.number = 5;
ctrl.factorial = function(num) {
if (num === 1) {
return 1;
} else {
return num * ctrl.factorial(num-1)
}
} // factorial
}
The number is just staying the way it is, its factorial not being shown. How do I fix this? I'm really lost.