AngularJS:ng-repeat中的控制器 - 如何访问变量

时间:2022-08-26 00:02:51

I need to access variable from ng-repeat inside controller, which is inside ng-repeat, code provided below. Unfortunately it won't work for me.

我需要从ng-repeat内部控制器访问变量,这是在ng-repeat内部,下面提供的代码。不幸的是,这对我不起作用。

<div ng-controller="ParentCtrl">
    <div ng-repeat="r in results">
        <div ng-controller="SomeCtrl">
            <p>{{r.something}}</p>   ($parent.r.something won't work either)
        </div>
    </div>
</div>

Is it possible? And if possible - how to do it?

可能吗?如果可能的话 - 怎么做?

EDIT: The thing I need is to make ng-repeat within one controller, to make list of available desks (in parent controller I have searching, filters etc) and then, when I click the button in chosen row of list (results), I need to take it's (row's) parameters and do something with them INSIDE second (child?) controller.

编辑:我需要的是在一个控制器中进行ng-repeat,制作可用的办公桌列表(在父控制器中我有搜索,过滤等)然后,当我点击所选行列表中的按钮(结果)时,我需要拿它(行)的参数并用它们INSIDE second(child?)控制器做一些事情。

3 个解决方案

#1


Use the ng-repeat variable ($index) in the html and call a function on the controller

在html中使用ng-repeat变量($ index)并在控制器上调用一个函数

<div ng-repeat="r in results">
    <div ng-controller="SomeCtrl">
        <p>{{r.doSomething($index)}}</p> 
    </div>
</div>

#2


ng-repeat should be nested in the ng-controller if you want to access variables in ng-controller. Lets assume you have "results" defined as an object in SomeCtrl, you will be able to access it with an ng-repeat. Example:

如果要访问ng-controller中的变量,ng-repeat应嵌套在ng-controller中。假设您在SomeCtrl中将“结果”定义为对象,您将能够使用ng-repeat访问它。例:

In your controllers.js

在你的controllers.js中

appControllers.controller('SomeCtrl', function(){
      $scope.results = someData;
});

In your view,

在你看来,

<div ng-controller="SomeCtrl">
   <div ng-repeat="r in results">
      <p>{{r.property}}</p>
   </div>
</div>

#3


Another solution would be to use the controller as syntax. See plunker for details.

另一种解决方案是使用控制器作为语法。有关详细信息,请参阅plunker。

<div ng-controller="ParentCtrl as parent">
    <div ng-repeat="r in parent.results">
        <div ng-controller="SomeCtrl as child">
            <p>{{child.something(r)}}</p>
        </div>
    </div>
</div> 

The recommended way to communicate across controller is via services. But in your case, I am not sure whether creating a cross cutting service would solve anything.. It would be something to think about though

跨控制器进行通信的推荐方法是通过服务。但在你的情况下,我不确定创建一个跨领域服务是否可以解决任何问题。但是,这是值得考虑的事情

#1


Use the ng-repeat variable ($index) in the html and call a function on the controller

在html中使用ng-repeat变量($ index)并在控制器上调用一个函数

<div ng-repeat="r in results">
    <div ng-controller="SomeCtrl">
        <p>{{r.doSomething($index)}}</p> 
    </div>
</div>

#2


ng-repeat should be nested in the ng-controller if you want to access variables in ng-controller. Lets assume you have "results" defined as an object in SomeCtrl, you will be able to access it with an ng-repeat. Example:

如果要访问ng-controller中的变量,ng-repeat应嵌套在ng-controller中。假设您在SomeCtrl中将“结果”定义为对象,您将能够使用ng-repeat访问它。例:

In your controllers.js

在你的controllers.js中

appControllers.controller('SomeCtrl', function(){
      $scope.results = someData;
});

In your view,

在你看来,

<div ng-controller="SomeCtrl">
   <div ng-repeat="r in results">
      <p>{{r.property}}</p>
   </div>
</div>

#3


Another solution would be to use the controller as syntax. See plunker for details.

另一种解决方案是使用控制器作为语法。有关详细信息,请参阅plunker。

<div ng-controller="ParentCtrl as parent">
    <div ng-repeat="r in parent.results">
        <div ng-controller="SomeCtrl as child">
            <p>{{child.something(r)}}</p>
        </div>
    </div>
</div> 

The recommended way to communicate across controller is via services. But in your case, I am not sure whether creating a cross cutting service would solve anything.. It would be something to think about though

跨控制器进行通信的推荐方法是通过服务。但在你的情况下,我不确定创建一个跨领域服务是否可以解决任何问题。但是,这是值得考虑的事情