![](/img/trans.png)
[英]What is the best way to keep track of changes of a document's property in MongoDB?
[英]What's the best way to keep an object property linked to a value?
我的問題與我重構的指令有關:
HTML
<!-- Before -->
<slider value="sliderValue" floor="0" ceil="maxPrice"></slider>
<!-- New version -->
<slider value="sliderValue" options="sliderOptions"></slider>
<!-- Both directives use '=' bindings... -->
JS
$scope.sliderValue=0;
$scope.maxPrice = 1000;
$scope.sliderOptions = {
floor: 0,
ceil: $scope.maxPrice
};
//later
$scope.maxPrice = 2000;
使用舊版本,我可以更新$scope.maxPrice
,它將自動更新指令中ceil
的作用域值。 我想在新版本中實現相同的目的。
我想到的第一個主意是在$scope.maxPrice
上使用$watch
,但是我讀過很多人解釋說,使用$watch
是不好的做法。
然后,我可以在每次更新$scope.sliderOptions.ceil
手動更新$scope.maxPrice
但這很煩人。
注意: $scope.maxPrice
是在應用程序的多個位置中使用的業務邏輯值,而$scope.sliderOptions
僅用於滑塊指令。
經過思考和討論,我想您僅限於將maxprice
在一個簡單的對象中。 這樣做的原因是因為JavaScript無法通過引用傳遞數字。 有關主題的更多信息: 是否有類似JavaScript中按值傳遞和引用傳遞的東西?
擬議的解決方案
$scope.sliderValue=0;
$scope.maxPrice = {value: 1000};
$scope.sliderOptions = {
floor: 0,
ceil: $scope.maxPrice
};
//later
$scope.maxPrice.value = 2000;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.