简体   繁体   English

AngularJS:针对特定“ID”的ng-repeat?

[英]AngularJS: ng-repeat for a specific “ID”?

first of all, I'm quite new to angularJS, so be gentle! 首先,我对angularJS很新,所以要温柔! Second, I know there are no "ID"s, "class"es etc. in angularJS so the title might be somewhat misleading. 其次,我知道angularJS中没有“ID”,“class”等,所以标题可能有点误导。 However, I failed to describe the issue any better. 但是,我没有更好地描述这个问题。

Here's the deal: I have an array of elements, and each has its details. 这是交易:我有一系列元素,每个都有它的细节。 Now, I ng-repeat the first set of elements to show the basic info. 现在,我重复第一组元素来显示基本信息。 And when user clicks "details", the corresponding details are loaded. 当用户点击“详细信息”时,会加载相应的详细信息。 Yet, in my case, each item gets the details of a selected element. 然而,就我而言,每个项目都会获得所选元素的详细信息。

The case in practice: http://jsbin.com/zilayija/2/edit 实践中的案例: http//jsbin.com/zilayija/2/edit

Is there any way to append the corresponding details to the ones that match Id ONLY? 有没有办法将相应的细节附加到仅与Id匹配的细节?

I guess you want to archive a master/detail view. 我想你想归档一个主/详细视图。 You need to place the displaying of the details outside the ng-repeat . 您需要在ng-repeat之外显示详细信息。 You also do not need an array for the details. 您还不需要数组来获取详细信息。 Just use an object on the scope and assign it in getDetails . 只需使用作用域上的对象并在getDetails指定它。

http://jsbin.com/heyaqexa http://jsbin.com/heyaqexa

The real issue here is that you're binding the details to a single array on your $scope, namely details . 这里真正的问题是你将细节绑定到$ scope上的单个数组,即details As this reference is used in each iteration of the ng-repeat, you'll end up showing it everywhere. 由于此引用用于ng-repeat的每次迭代,因此您最终会在任何地方显示它。

Edit : As mentioned in the comments, the original question did not reflect what was actually asked. 编辑 :正如评论中所提到的,原始问题没有反映出实际问题。 What was requested was that the details needed to be loaded in asynchronously and only when they were requested (clicked). 要求的是,细节需要异步加载,并且仅在请求(单击)时才加载。 I therefore updated the code example ). 因此,我更新了代码示例 )。

What changed? 改变了什么?

HTML: HTML:

<li ng-repeat="item in items">
    {{item.name}}
    <a href="" ng-click="item.showDetails()">show details</a>
    <p ng-show="item.detailsVisible" ng-repeat="detail in getDetails(item.id)">
        {{detail.belongsToItem}}
    </p>
</li>

JS: JS:

var Item = function(id, name){
    var promiseDetailsAreLoaded;
    var self = this;

    this.id = id;
    this.name = name;
    this.detailsVisible = false;
    this.details = [];
    this.showDetails = function(){
        if(!promiseDetailsAreLoaded){
          promiseDetailsAreLoaded = $http.get("").then(function(){
              // mock adding details
              for(var i = 0; i < itemDetails.length; i++){
                  if(itemDetails[i].belongsToItem === self.id){
                      self.details.push(itemDetails[i]);
                  }
              }
          });
        }
        promiseDetailsAreLoaded.then(function(){
            self.detailsVisible = !self.detailsVisible;  
        });
    }
};

$scope.items = [
  new Item(1, "Item 1"),
  new Item(2, "Item 2"),
  new Item(3, "Item 3")
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM