|
var app = angular.module('mortgageApp', []); |
|
|
|
app.controller('headerCtrl', function($scope){ |
|
$scope.header = "Angular Mortgage Calculator"; |
|
$scope.mySite = "Petrus Rex"; |
|
$scope.formula = "M = P[i(1+i)^n]/[(1+i)^n -1]"; |
|
}); |
|
|
|
app.controller('appCtrl',['$scope', function($scope){ |
|
$scope.mortgage = { |
|
homePrice: 100000, |
|
downPayment: 0, |
|
newHomeAmt: 0, |
|
interestRate: 5, |
|
aprMonthlyRate: 0, |
|
numberOfPayments: 0, |
|
interestPayments: 0, |
|
mortgagePeriod: 15, |
|
monthlyPayment: 0 |
|
} |
|
|
|
//subtract down payment from home price |
|
var findNewHomeAmt = function(){ |
|
$scope.mortgage.newHomeAmt = $scope.mortgage.homePrice - $scope.mortgage.downPayment; |
|
} |
|
$scope.$watch('mortgage.homePrice + mortgage.downPayment',findNewHomeAmt); |
|
|
|
//Calculate monthly APR Rate |
|
var aprMonthly = function(){ |
|
$scope.mortgage.aprMonthlyRate = (($scope.mortgage.interestRate / 100) / 12); |
|
}; |
|
$scope.$watch('mortgage.interestRate', aprMonthly); |
|
|
|
//Calculate Total Number of Mortgage Payments |
|
var numOfPayments = function(){ |
|
$scope.mortgage.numberOfPayments = ($scope.mortgage.mortgagePeriod * 12); |
|
}; |
|
$scope.$watch('mortgage.mortgagePeriod', numOfPayments); |
|
|
|
//Calculate term (1+i)^n or interestPayments^numberOfPayments |
|
var interestPayments = function(){ |
|
$scope.mortgage.interestPayments = Math.pow(1 + $scope.mortgage.aprMonthlyRate, $scope.mortgage.numberOfPayments); |
|
}; |
|
$scope.$watch('mortgage.mortgagePeriod + mortgage.interestRate', interestPayments); |
|
|
|
//calculate monthly mortgage payment |
|
var monthlyPayment = function(){ |
|
$scope.mortgage.monthlyPayment = ($scope.mortgage.newHomeAmt * ($scope.mortgage.aprMonthlyRate * $scope.mortgage.interestPayments) / ($scope.mortgage.interestPayments - 1)); |
|
}; |
|
$scope.$watch('mortgage.aprMonthlyRate + mortgage.interestPayments + mortgage.downPayment + mortgage.homePrice', monthlyPayment); |
|
}]); |