The variant with the function does work. But you also could pre-calculate the value (this might be faster if you use priceperkilometer often):
$scope.kilometer = 20;
$scope.carType = 1;
calculatePrice = function() {
if ($scope.kilometer < 20 and $scope.carType=1)
return 10;
else if ($scope.kilometer < 20 and $scope.carType=2)
return 20;
else if ($scope.kilometer >= 20 and $scope.carType=1)
return 30;
else if ($scope.kilometer >= 20 and $scope.carType=2)
return 40;
};
$scope.priceperkilometer = calculatePrice();
$scope.$watch('kilometer', function(newValue, oldValue) {
if (newValue != oldValue)
$scope.priceperkilometer = calculatePrice();
});
$scope.$watch('carType', function(newValue, oldValue) {
if (newValue != oldValue)
$scope.priceperkilometer = calculatePrice();
});