将信息传递给ng-repeat ng-include范围

时间:2022-12-02 17:28:14
  • I'm pulling my hair about a particular issue with my AngularJS application.
  • 我在为我的AngularJS应用程序的一个特殊问题而烦恼。

Basically, I have a controller, "PageCtrl", which pulls in a list of data via a service. The data is saved inside the PageCtrl. All works fine - so far so good.

基本上,我有一个名为“PageCtrl”的控制器,它通过服务获取数据列表。数据保存在PageCtrl中。到目前为止一切都很好。

Now my issue: for each object of the data inside the PageCtrl, I use ng-repeat to display each data just fetched.

现在我的问题是:对于PageCtrl中的每个数据对象,我使用ng-repeat来显示刚刚获取的每个数据。

In each of the ng-repeats, I use ng-include to get the correct template, depending of which data is in each object (basically the data is a list of elements that is to be displayed on the page. Each element has it's own controller.

在每一个ng-repeat中,我使用ng-include来获得正确的模板,这取决于每个对象中的数据(基本上,数据是要显示在页面上的元素的列表)。每个元素都有自己的控制器。

As ng-include creates a newscope, all the created child scopes/controllers have access to the data, as it is located in the $parent controller - PageCtrl.

当ng-include创建一个新范围时,所有创建的子范围/控制器都可以访问数据,因为数据位于$parent controller - PageCtrl中。

All works fine, except that each created controller in ng-repeat/ng-include needs to know which object in list of data in the parent, it needs to use to display.

除了在ng-repeat/ng-include中创建的每个控制器需要知道父数据列表中的哪个对象之外,所有这些都可以正常工作。

Basically, in my ng-repeat, I would like to use the $index and pass it to the controller included in the ng-include. But I cannot do it! If I use ng-init, that code inside ng-init will be executed on the parent scope, ie. my original PageCtrl.

基本上,在我的ng-repeat中,我希望使用$index并将其传递给包含在ng-include中的控制器。但我做不到!如果我使用ng-init, ng-init中的代码将在父范围内执行。我原来PageCtrl。

How can I, without the use of directives, pass information to the controller so that it knows which of the data-objects in its parent it should use?

如何在不使用指令的情况下,将信息传递给控制器,使其知道应该使用其父对象中的哪个数据对象?

Here is the code that loops over the data in MainCtrl:

下面是在MainCtrl中对数据进行循环的代码:

 <div ng-repeat="element in elements" >
      <ng-include src=" GetTemplateFromElement(element.type) " ng-init=" SetElementNo($index); "></ng-include>
 </div>

In the snippet, I call GetTemplateFromElement on the MainCtrl which returns the view to include. In the included view I refer which controller should do the work. I try to use $index to tell the included controller which index in the data it should use. But ofcourse the code is executed in MainCtrl scope. Example of a view:

在代码片段中,我调用MainCtrl中的GetTemplateFromElement,它返回视图以包含。在包含的视图中,我引用哪个控制器来做这项工作。我尝试使用$index来告诉所包含的控制器应该在数据中使用哪个索引。当然,代码是在MainCtrl作用域中执行的。一个视图的例子:

<div ng-controller="ElementDocumentCtrl">
    <div ng-bind-html-unsafe="element.document_content"></div>
 </div>

In the snippet, "element.document_content" is on the scope of ElementDocumentCtrl, which pulls the data from its parent, PageCtrl.

片段的元素。document_content”位于ElementDocumentCtrl的范围内,该范围从其父PageCtrl中提取数据。

How can I tell the directives included which index in the data-list in PageCtrl (it's parent controller) it should pick up?

我怎样才能告诉包含在PageCtrl(它的父控制器)的数据列表中的哪个索引的指令?

Please don't tell me I need to make them all directives :)

请不要告诉我,我需要把它们都指示给你。

Thanks, guys.

谢谢,伙计们。

1 个解决方案

#1


3  

Maybe Im not understanding your question completely, but from the docs, $index, $first, $middle and $last are all exposed on the local scope of the ng-repeat children.

也许我没有完全理解你的问题,但是从文档,$index, $first, $middle和$last都暴露在ng-repeat children的本地范围内。

Ive put in a plunker to demonstrate this. The ChildCtrl accesses the $index from the parent to get the index of which element in the parent it is accessing.

我放了一个活塞来演示这个。ChildCtrl从父元素访问$index,以获取它正在访问的父元素的索引。

#1


3  

Maybe Im not understanding your question completely, but from the docs, $index, $first, $middle and $last are all exposed on the local scope of the ng-repeat children.

也许我没有完全理解你的问题,但是从文档,$index, $first, $middle和$last都暴露在ng-repeat children的本地范围内。

Ive put in a plunker to demonstrate this. The ChildCtrl accesses the $index from the parent to get the index of which element in the parent it is accessing.

我放了一个活塞来演示这个。ChildCtrl从父元素访问$index,以获取它正在访问的父元素的索引。