将父范围值传递到 Angular 中的 ng-repeat 循环中 [英] Pass parent scope value into ng-repeat loop in Angular

查看:19
本文介绍了将父范围值传递到 Angular 中的 ng-repeat 循环中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是一个非常简单的问题,但我发现的所有解决方法都很复杂.我在模板中使用 ng-repeat 循环遍历对象数组,如下所示:

{{ message.subject }} ... {{ campaign.name }} ...

由于 ng-repeat 创建了一个新的作用域,控制器中的campaign"对象似乎无法访问.有什么方法(除了将广告系列对象添加到数组中的每个项目之外)获得该值?

提前致谢.

解决方案

您可以使用 $parent 访问父作用域

{{ message.subject }} ... {{ $parent.campaign.name }} ...

This should be an extremely simple question, but all of the workarounds I've found are complex. I'm looping through an array of objects in using ng-repeat in a template as follows:

<div class="row-fluid" ng-repeat="message in messages.current|filter:'draft'"> 
    {{ message.subject }} ... {{ campaign.name }} ...
</div>

Since the ng-repeat creates a new scope, the 'campaign' object from the controller doesn't seem to be accessable. Is there any way (aside from adding the campaign object to every item in my array) of getting that value?

Thanks in advance.

解决方案

You can access the parent scope by using $parent

<div class="row-fluid" ng-repeat="message in messages.current|filter:'draft'"> 
    {{ message.subject }} ... {{ $parent.campaign.name }} ...
</div>

这篇关于将父范围值传递到 Angular 中的 ng-repeat 循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆