简体   繁体   English

我可以使用ng-repeat并使用角度分量隔离范围吗?

[英]Can I use ng-repeat and have isolated scope with angular component?

So I have a ng repeat block that passes an object to a angular component. 所以我有一个ng重复块,它将一个对象传递给一个角度组件。 That looks like this 看起来像这样

<div ng-repeat="assessor in $ctrl.dedupeDetail.matches">
    <assessordedupequickcard assessor="assessor"></assessordedupequickcard>
</div>

Edit: 编辑:

I thought it might be helpful to add what the component is defined as. 我认为添加组件定义的内容可能会有所帮助。 Here is that code 这是代码

angular.module('assessor.dedupe')
    .component('assessordedupequickcard', {
        controller: 'assessorDedupeQuickcardController',
        templateUrl: 'src/app/assessor/dedupe/quickcard/assessor.dedupe.quickcard.html',
        bindings: {
            assessor: '<',
        }
    });

End Edit 结束编辑

The quickcard takes the various fields in the assessor object such as name, address, phone numbers, age, etc, and displays them cleanly in a small box that is supposed to represent a contact card for that person. 快速卡采用评估者对象中的各个字段,例如姓名,地址,电话号码,年龄等,并将它们干净地显示在一个小盒子中,该盒子应该代表该人的联系卡片。

In that quickcard are two labels 在那张quickcard中有两个标签

<label ng-if="$ctrl.isSource" class="quickcard-list-header pull-right">FROM</label>
<label ng-if="$ctrl.isTarget" class="quickcard-list-header pull-right">TO</label>

In the view where the list of these, "contact cards" is displayed there is a functionality where you can select one card, then select another, and transfer information from one to the other. 在显示这些“联系人卡片”列表的视图中,您可以选择一张卡,然后选择另一张卡,并将信息从一张卡传输到另一张卡。

When selecting the first 选择第一个时

<assessordedupequickcard assessor="assessor"></assessordedupequickcard>

from the ng-repeated list I need to flip a flag that exists within that < assessordedupequickcard > component's scope in its controller and flip isSource = true. 从ng-repeated列表中,我需要在其控制器中翻转存在于<assessordedupequickcard>组件范围内的标志,并翻转isSource = true。

And when selecting the second 选择第二个时

<assessordedupequickcard assessor="assessor"></assessordedupequickcard>

from the ng-repeated list, again move one layer deeper into that custom component's controller and flip its isTarget = true. 从ng-repeated列表中,再次将一层深入到该自定义组件的控制器中并翻转其isTarget = true。

But I cannot figure out how to isolate which component will have a flag flip. 但我无法弄清楚如何隔离哪个组件将有一个标志翻转。 Because they are not individually named they are all treated as one. 因为它们没有单独命名,所以它们都被视为一个。 Any change I make to one of the repeated custom components happens to all of the custom components. 我对其中一个重复的自定义组件所做的任何更改都会发生在所有自定义组件上。

I'm not sure I've explained this well, if there's any more info I can provide please let me know. 我不确定我已经解释得很好,如果我能提供更多信息请告诉我。 Thanks in advance for any help anyone can provide me in regards to this. 在此先感谢任何人可以提供给我的任何帮助。

Angular components always have isolated scope ( components documentation ). 角度组件始终具有隔离范围( 组件文档 )。 Now, you just need to implement two-way binding and pass the data object. 现在,您只需要实现双向绑定并传递数据对象。 This makes the controller (that contains repeated components) aware of any data changes occurring within each components distinctly. 这使得控制器(包含重复组件)清楚地意识到每个组件内发生的任何数据更改。

Page HTML: 页面HTML:

<div ng-controller="SampleCtrl">
  <h3>Repeated Components with Isolated Scope: </h3>
  <box ng-repeat="data in collection track by $index" data="data"></box>

  <h3>Parent Controller:</h3>
  <pre>{{collection| json}}</pre>
</div>

Page JS: 页面JS:

.controller('SampleCtrl', function($scope) {
  $scope.collection = [{
    name: 'A'
  }, {
    name: 'B'
  }, {
    name: 'C'
  }, {
    name: 'D'
  }, {
    name: 'E'
  }];

})

Component JS: 组件JS:

.component('box', {
    bindings: {
      data: '=?'
    },
    templateUrl: 'box.html',
    controllerAs: 'vm',
    controller: function() {
      var vm = this;
      vm.toggle = function() {
        vm.data.flagged = !vm.data.flagged;
      }
    }
  });

Component HTML: 组件HTML:

<div class="box" ng-click="vm.toggle()">
  <p>{{vm.data}}</p>
</div>

Plunker Demo Plunker演示

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

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