观察范围对象包含特殊字符作为键

时间:2021-08-22 15:29:59

I am trying to implement a filter for AngularJS ng-grid.

我正在尝试为AngularJS ng-grid实现一个过滤器。

AngularJS Filter Example

AngularJS过滤器示例

Issue Fiddle

function app($scope){
    $scope.filter = {};
    $scope.filter.$ = '';
    $scope.filter.name = '';
    $scope.filter.phone = '';

    $scope.$watch('filter', function(new_value, old_value){
        $scope.filtered = JSON.stringify(new_value);
    }, true);
}

Problem

Watch works for name and phone but not for $. I am not getting the problem, can someone help me out?

手表适用于名称和手机,但不适用于$。我没有遇到问题,有人可以帮帮我吗?

1 个解决方案

#1


2  

This happens because when angular does the dirty-checking to see if the new value is different than the last value, it ignores properties starting with '$':

发生这种情况是因为当angular执行脏检查以查看新值是否与最后一个值不同时,它会忽略以'$'开头的属性:

function equals(o1, o2) {
    ...
    for (key in o1) {
        if (key.charAt(0) === '$' || isFunction(o1[key])) continue;
        ...

Thus, it will not detect any change if only $scope.filter.$ has changed and it will not fire the watcher callback.

因此,如果只有$ scope.filter。$已更改并且它不会触发观察者回调,它将不会检测到任何更改。


This is an obvious design decision, since properties starting with $ are considered "angular-specific", but watching a filter-object also seems reasonable.
I would open an issue on GitHub and see what the core team things about it. Maybe they will decide to make an exception for a $ property or something.

这是一个明显的设计决策,因为以$开头的属性被认为是“特定于角度的”,但是观看过滤器对象似乎也是合理的。我会在GitHub上打开一个问题,看看核心团队的内容是什么。也许他们会决定对$ property或者其他东西做例外。

#1


2  

This happens because when angular does the dirty-checking to see if the new value is different than the last value, it ignores properties starting with '$':

发生这种情况是因为当angular执行脏检查以查看新值是否与最后一个值不同时,它会忽略以'$'开头的属性:

function equals(o1, o2) {
    ...
    for (key in o1) {
        if (key.charAt(0) === '$' || isFunction(o1[key])) continue;
        ...

Thus, it will not detect any change if only $scope.filter.$ has changed and it will not fire the watcher callback.

因此,如果只有$ scope.filter。$已更改并且它不会触发观察者回调,它将不会检测到任何更改。


This is an obvious design decision, since properties starting with $ are considered "angular-specific", but watching a filter-object also seems reasonable.
I would open an issue on GitHub and see what the core team things about it. Maybe they will decide to make an exception for a $ property or something.

这是一个明显的设计决策,因为以$开头的属性被认为是“特定于角度的”,但是观看过滤器对象似乎也是合理的。我会在GitHub上打开一个问题,看看核心团队的内容是什么。也许他们会决定对$ property或者其他东西做例外。