1

i'm trying to run basic math functions on bound scope objects in my controller. The view is a series of sliders that will need to react based on the user input of a few different sliders. I can't seem to get the values to live update on the front end.

This is my 'calculations' for those values.

$scope.mainSlide = {
    assets: ($scope.slide1.assets * $scope.slide2.assets / .1),
    roa: ($scope.slide1.roa * $scope.slide2.roa / .1)
};

Im a bit confused why the computed values aren't being bound on the front end.

Any help, or direction would be greatly appreciated!

4

1 回答 1

4

对于 上的计算属性$scope,您可以使用$scope.$watch在值更改时重新计算并设置新值(例如 watch slide1 和 slide2,在它们更改时设置 mainslide 的值)。您还可以将 mainSlide 的计算值设置为函数,它们将自动更新:

在你的情况下:

$scope.mainSlide = {
  assets: function() { return ($scope.slide1.assets * $scope.slide2.assets / .1);},
  roa: function() { return ($scope.slide1.roa * $scope.slide2.roa / .1);}
};

要在您的视图中使用它们:

Assets: {{mainSlide.assets()}}
Roa: {{mainSlide.roa()}}

函数方法的示例 Plunker:http ://plnkr.co/edit/tFiZ5z?p=preview

$watch 方法的示例 Plunker:http ://plnkr.co/edit/5iASgJ?p=preview

于 2013-10-10T16:38:40.293 回答