在變量上創建監視表達式。一個自然的地方做,這是在控制器 - 有點像:
var updatePrice = function(){
//you might have to do null checks on the scope variables
$scope.project.price = $scope.project.hours * $scope.project.rate;
}
$scope.$watch('project.hours',updatePrice);
$scope.$watch('project.rate',updatePrice);
另一種可能性是使用上的輸入字段的ngChange指令:
$scope.updatePrice = updatePrice;
<input type='number' name='hours' ng-model='project.hours' ng-change="updatePrice()" />
<input type='number' name='rate' ng-model='project.rate' ng-change="updatePrice()" />