I've created this plunkr that tries to capture your question:
http://plnkr.co/edit/02QAC8m9xyF4pSyxnfOf
Basically, any code that depends on a value that can change should be in a watch. This means that your initialization code for setting taxflag belongs in a watch, so that it can update the tax flag if things change. This looks something like:
$scope.$watch('invoice.taxtotal', function(taxTotal) {
if (!angular.isNumber(taxTotal)) {
$scope.taxflag = false
return;
}
if (taxTotal > 0) {
$scope.taxflag = true;
} else {
$scope.taxflag = false;
}
});
Keep in mind watches are always executed the first time to initialize the values, so they essentially serve as both initialization code and update code.
As far as your calculate_grand_total
function, if it is up to you to define what it should return if tax total or invoice are undefined. Simply check whether it is undefined and return the appropriate value, for example in my plunkr I return empty string:
$scope.calculate_grand_total = function() {
if ($scope.invoice && angular.isNumber($scope.invoice.taxtotal)) {
return $scope.invoice.taxtotal + 5;
} else {
return "";
}
}
I'm not sure if my plunkr works exactly like you want it or not, but it should get you started in the right direction. Or serve as a starting point for further clarification.