【问题标题】:Get data inside an ng-repeat from an directive outside of it从 ng-repeat 外部的指令中获取数据
【发布时间】:2014-07-24 13:42:19
【问题描述】:

我正在使用基于 AngularJS 的 Ionic 框架开发移动应用程序。

在一个指令上,我正在循环使用 ng-repeat 的 JSON 数组,并应用 ng-if 的条件,见下文:

<ion-content class="has-header has-subheader" >

<ion-slide-box">
<ion-slide
    ng-if="invocation.title_id == titleid" 
    ng-repeat="invocation in invocations" >

    <h3>{{invocation.id}}</h3>
    <div ><h1>{{invocation.invocation_frensh }}</h1></div>
    <div ><h1>{{invocation.invocation_ar}}</h1></div>
    <div ><h1>{{invocation.invocation_franko}}</h1></div>
    <div ><h1>{{invocation.comments_fr}}</h1></div>
    <div ><h1>{{invocation.comments_ar}}</h1></div>
  </content>
</ion-slide>
</ion-slide-box>

<p>{{invocation.id}}</p>

</ion-content>

关键是最后一个p什么都没有显示。

我知道 $scope 不一样,但在最后一个 "p" 或 ng-repeat 之外的任何其他组件上,我需要拥有相同的数据才能与之交互。

例如,我想在获取"{{invocation.id}}" 的页脚上添加一个按钮。如果 ng-repeat "h3" 中的 invocation.id 等于 3 我希望它在 "p" 中等于 3

我该怎么做?

感谢您的帮助

编辑: 事实上,我希望 invocations[index].id 在循环外的“p”中,其中 index 等于显示的幻灯片。

【问题讨论】:

  • 调用在循环之外没有意义。你想在 p 标签中引用哪个调用对象?
  • 如果重复多次怎么办 - 你想在页脚中显示哪个 invocation.id?最后一个?
  • 感谢 cmets。我想看到与循环中显示的相同的 invocation.id。我应该在离子载玻片上说得更具体一些吗?
  • 事实上,我希望在循环外的“p”中调用 [index].id,其中 index 等于显示的幻灯片。

标签: angularjs angularjs-ng-repeat ionic-framework


【解决方案1】:

invocation 变量在ng-repeat 结束时消失,如果您需要再次使用它,您将需要另一个ng-repeat

【讨论】:

    【解决方案2】:

    假设您一次只显示一张幻灯片,您可以不使用ng-if,而是使用过滤器。

    <ion-slide ng-repeat="invocation in displayedInvocations = (invocations | filter:{title_id: title_id}:true)">
    

    然后在外面:

    <p>{{displayedInvocations[0].id}}</p>
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-30
      • 1970-01-01
      • 2014-09-25
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多