简体   繁体   English

将对象属性链接到值的最佳方法是什么?

[英]What's the best way to keep an object property linked to a value?

My question is related to a directive that I have refactored: 我的问题与我重构的指令有关:

HTML HTML

<!-- Before -->
<slider value="sliderValue" floor="0" ceil="maxPrice"></slider>

<!-- New version -->
<slider value="sliderValue" options="sliderOptions"></slider>

<!-- Both directives use '=' bindings... -->

JS JS

$scope.sliderValue=0;
$scope.maxPrice = 1000;

$scope.sliderOptions = {
    floor: 0,
    ceil: $scope.maxPrice
};

//later
$scope.maxPrice = 2000;

With the old version, I could update $scope.maxPrice , and it would automatically update the scope value of ceil in the directive. 使用旧版本,我可以更新$scope.maxPrice ,它将自动更新指令中ceil的作用域值。 I would like to achieve the same with the new version. 我想在新版本中实现相同的目的。

The first idea to come to my mind is using a $watch on $scope.maxPrice , but I have read many people explaining that using $watch is a bad practice. 我想到的第一个主意是在$scope.maxPrice上使用$watch ,但是我读过很多人解释说,使用$watch是不好的做法。

Then, I could update $scope.sliderOptions.ceil manually every time I update $scope.maxPrice but this is quite annoying. 然后,我可以在每次更新$scope.sliderOptions.ceil手动更新$scope.maxPrice但这很烦人。

NB $scope.maxPrice is a business-logic value used in several places of the application while $scope.sliderOptions is only used for the slider directive. 注意: $scope.maxPrice是在应用程序的多个位置中使用的业务逻辑值,而$scope.sliderOptions仅用于滑块指令。

After giving it some thought and discussion I guess you're limited to wrapping maxprice in an simple object. 经过思考和讨论,我想您仅限于将maxprice在一个简单的对象中。 The reason to do so would be because JavaScript can't pass numbers by reference. 这样做的原因是因为JavaScript无法通过引用传递数字。 More information on the topic: Is there thing like pass by value pass by reference in JavaScript? 有关主题的更多信息: 是否有类似JavaScript中按值传递和引用传递的东西?

Proposed solution 拟议的解决方案

$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.

相关问题 在MongoDB中跟踪文档属性更改的最佳方法是什么? - What is the best way to keep track of changes of a document's property in MongoDB? 更改javascript对象数组中的对象属性的最佳方法是什么? - what's the best way to change an object property in a javascript object array? 在javascript对象中,获取值属性的最佳方法是什么? - in a javascript object, what's best way to get the attribute of a value? 按特定值对 JSON 对象进行分组的最佳方法是什么? - What's the best way to group a JSON object by a certain value? 更改属性值的最佳方法。 它在对象数组的对象内 - Best way to change a value of a property. It's within an object of an array of objects 检查值是时间戳的最佳方法是什么? - What's the best way of checking that a value is a timestamp? 进行原型制作时跟踪javascript对象引用的最佳方法是什么 - What the best way to keep track of a javascript object reference when prototyping 跟踪实时通知发送给用户的最佳方法是什么? - What's the best way to keep track of live notifications to send to a user? 将键值对添加到数组中存在的 JS object 的最佳方法是什么 - What is the best way to add key-value pair(s) to a JS object present in an array 用属性检查数组对象是否存在的最佳方法是什么? - What is the best way to check for existence of array object with property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM