ngrepeat 不迭代 $ 属性
ngrepeat does not iterate $ attribute
我尝试使用对象过滤并通过对象中的 ng-repeat = (key, value)
打印出过滤器。
当我尝试各种 filters 时,我发现 ng-repeat
似乎不适用于对象的 $
属性,这在过滤时非常有用。
是否可以使用$
自动显示筛选对象的所有属性
This link 表明它似乎不适用于以 $
开头的对象
$scope.testObj = {};
$scope.testObj.test = 'test';
$scope.testObj.$ = '
;
$scope.testObj.$test = '$test';
<div ng-repeat = "(key, value) in testObj">
<p>{{key}}: {{value}}</p>
</div>
看起来 ng-repeat
过滤掉以 $
.
开头的对象属性
来自the source:
for (var itemKey in collection) {
if (collection.hasOwnProperty(itemKey) && itemKey.charAt(0) != '$') {
collectionKeys.push(itemKey);
}
}
这很可能是因为 Angular 使用 $
来指示 内部代码 Angular 库。
这似乎只有在对对象使用 ng-repeat
时才会发生。
我看到代码中明确禁止使用它们:
https://github.com/angular/angular.js/blob/master/src/ng/directive/ngRepeat.js#L341
周围没有评论,我同意这对我来说与其说是一个功能,不如说是一个错误。这不是将属性标记为不可枚举和 Object.keys
的目的吗?
浏览器兼容性地狱,可能是一如既往的原因。
AngularJS暂不支持。有一个open issue on Github.
但是您可以使用一些代码使其工作:
app.controller('MainCtrl', function($scope) {
var getProperties = function(input){
var result = [];
for(var propertyName in input) {
result.push({key : propertyName, value : input[propertyName]});
}
return result;
};
$scope.testObj = {};
$scope.testObj.test = 'test';
$scope.testObj.$ = '$';
$scope.testObj.$whatever = '$whatever';
$scope.testObjProperties = getProperties($scope.testObj);
});
然后在您的视图中显示它:
<div ng-repeat="property in testObjProperties">
<p>{{property.key}} : {{property.value}}</p>
</div>
这是一个有效的插件:http://plnkr.co/edit/LFrfLcpoOg0ScEY89p25?p=preview
我尝试使用对象过滤并通过对象中的 ng-repeat = (key, value)
打印出过滤器。
当我尝试各种 filters 时,我发现 ng-repeat
似乎不适用于对象的 $
属性,这在过滤时非常有用。
是否可以使用$
This link 表明它似乎不适用于以 $
开头的对象 $scope.testObj = {};
$scope.testObj.test = 'test';
$scope.testObj.$ = '
;
$scope.testObj.$test = '$test';
<div ng-repeat = "(key, value) in testObj">
<p>{{key}}: {{value}}</p>
</div>
看起来 ng-repeat
过滤掉以 $
.
来自the source:
for (var itemKey in collection) {
if (collection.hasOwnProperty(itemKey) && itemKey.charAt(0) != '$') {
collectionKeys.push(itemKey);
}
}
这很可能是因为 Angular 使用 $
来指示 内部代码 Angular 库。
这似乎只有在对对象使用 ng-repeat
时才会发生。
我看到代码中明确禁止使用它们:
https://github.com/angular/angular.js/blob/master/src/ng/directive/ngRepeat.js#L341
周围没有评论,我同意这对我来说与其说是一个功能,不如说是一个错误。这不是将属性标记为不可枚举和 Object.keys
的目的吗?
浏览器兼容性地狱,可能是一如既往的原因。
AngularJS暂不支持。有一个open issue on Github.
但是您可以使用一些代码使其工作:
app.controller('MainCtrl', function($scope) {
var getProperties = function(input){
var result = [];
for(var propertyName in input) {
result.push({key : propertyName, value : input[propertyName]});
}
return result;
};
$scope.testObj = {};
$scope.testObj.test = 'test';
$scope.testObj.$ = '$';
$scope.testObj.$whatever = '$whatever';
$scope.testObjProperties = getProperties($scope.testObj);
});
然后在您的视图中显示它:
<div ng-repeat="property in testObjProperties">
<p>{{property.key}} : {{property.value}}</p>
</div>
这是一个有效的插件:http://plnkr.co/edit/LFrfLcpoOg0ScEY89p25?p=preview