【发布时间】:2015-03-09 13:13:14
【问题描述】:
在这个例子中,我有三个作用域对象
$scope.themes = [
{
name: 'Theme 1',
backgroundColor: '#000000',
},
{
name: 'Theme 2',
backgroundColor: '#FFFFFF',
},
];
$scope.theme = {
name: 'Default Theme',
backgroundColor: '#000000',
};
$scope.config = {
canvas: {
fill: $scope.theme.backgroundColor,
},
elements: [
{
name: 'Circle',
width: 200,
height: 1000,
fill: $scope.theme.backgroundColor
},
]
};
ng-select 更新$scope.theme 的值(使用$scope.themes 作为ng-options)。当$scope.theme 发生变化时,我想更新$scope.config 的值,其中一些属性依赖于$scope.theme 的值。
$scope.config 有一个 $watch() 通过指令在其上运行,该指令对视图中的元素进行更改,因为可以通过界面更改许多已编辑的属性。
当$scope.theme 更改时,如何触发对$scope.config 的更新,以触发我的$watch?
(值得注意的是,上面是$scope.config 的一个经过大量编辑的版本,其中config.elements 中有很多项目。)
【问题讨论】:
-
您是否意识到您根本不需要任何 $watch ,因为您可以使用对象引用?
-
使用引用来利用继承。喜欢:
$scope.theme = $scope.themes[0]...$scope.config{ someProperty: $scope.theme.propertyName}
标签: javascript angularjs scope watch