Javascript 如何使用angular在DOM中通过变量名访问对象的键
我有一个控制器,它向作用域中添加了一个名为Javascript 如何使用angular在DOM中通过变量名访问对象的键,javascript,angularjs,dom,Javascript,Angularjs,Dom,我有一个控制器,它向作用域中添加了一个名为comments $scope.showComments = function(id){ dataService.getComments(id).then(function(data){ $scope.comments[id] = data.comments; console.log($scope.comments); }); } 我想对特定id调用ng repeat。以下方法不起作用。有什么想法吗 &l
comments
$scope.showComments = function(id){
dataService.getComments(id).then(function(data){
$scope.comments[id] = data.comments;
console.log($scope.comments);
});
}
我想对特定id调用ng repeat。以下方法不起作用。有什么想法吗
<div class="commentsContainer" id="{{id}}">
<div class="eachcomment" ng-repeat="comment in comments.{{id}}">
<p>{{comment.content}}
</div>
</div>
{{comment.content}
所需的id被提供给外部分区。但是
ng repeat
不起作用。您应该使用过滤器来实现此功能,如下所示:
<div class="eachcomment" ng-repeat="comment in comments | filterId: id">
<p>{{comment.content}}
</div>
或者您可以使用一种更简单的方法:
<div class="eachcomment" ng-repeat="comment in comments | filter: checkId">
<p>{{comment.content}}
</div>
您应该使用过滤器来实现以下功能:
<div class="eachcomment" ng-repeat="comment in comments | filterId: id">
<p>{{comment.content}}
</div>
或者您可以使用一种更简单的方法:
<div class="eachcomment" ng-repeat="comment in comments | filter: checkId">
<p>{{comment.content}}
</div>
您可以通过以下方式实现:
<div ng-app="myApp" ng-controller="dummy">
<div class="commentsContainer" id="{{id}}">
<div class="eachcomment" ng-repeat="(key, comment) in comments">
<p ng-show="key === id">{{comment.content}}</p>
</div>
</div>
</div>
您可以通过以下方式实现:
<div ng-app="myApp" ng-controller="dummy">
<div class="commentsContainer" id="{{id}}">
<div class="eachcomment" ng-repeat="(key, comment) in comments">
<p ng-show="key === id">{{comment.content}}</p>
</div>
</div>
</div>
您应该使用变量键访问属性:
<div class="commentsContainer" id="{{id}}">
<div class="eachcomment" ng-repeat="comment in comments[id]">
<p>{{comment.content}}
</div>
</div>
{{comment.content}
您应该使用变量键访问属性:
<div class="commentsContainer" id="{{id}}">
<div class="eachcomment" ng-repeat="comment in comments[id]">
<p>{{comment.content}}
</div>
</div>
{{comment.content}
但这会给我一个ID为键的对象。如何访问与给定ID链接的内容。我事先不知道该ID。当您的ng repeat正在运行时,您的$scope.ID设置正确?这将在另一个ng repeat中运行。因此,我正在使用ng-init初始化outer repeat中每个元素的id。请创建一个Plunkers,但这将为我提供id为键的对象。如何访问与给定ID链接的内容。我事先不知道该ID。当您的ng repeat正在运行时,您的$scope.ID设置正确?这将在另一个ng repeat中运行。因此,我正在使用ng-init为外部重复中的每个元素初始化一个id。请创建一个plunker