简体   繁体   English

如何将项目从ng-repeat传递到ng-include创建/加载的控制器?

[英]How to pass an item from ng-repeat to a controller created/loaded by ng-include?

I have an ng-repeat that loads an ng-include. 我有一个ng-repeat加载ng-include。 I'd like to have a separate controller inside the view that the ng-include loads. 我想在ng-include加载的视图内有一个单独的控制器。

But I'm having trouble accessing the item from ng-repeat in that controller. 但是我在该控制器中无法从ng-repeat访问该项目。 I tried something like this, and it fails. 我尝试了类似的方法,但失败了。

How do I get at result inside of SubController ? 我如何在resultSubController

    <div ng-repeat="result in results">
            <div class="box" ng-include="view/someinclude.html"></div>
    </div>

view/someinclude.html: view / someinclude.html:

<div ng-controller="SubController">
     ...
</div>

controller js: 控制器js:

angular.module('SubController', [])
    .controller('SubController', ['$scope',
        function ($scope) {
            //fails
            console.log($scope.result);
        }
    ]);

确保您使用的是同一模块:

angular.module('SubController').controller('SubController', function() {...});

Make sure your scopes are a part of the same module, or they they're different include one in the other, if that's not the problem then it'll be something outside what you've shown, as something like this: 确保您的范围是同一模块的一部分,或者它们是不同的,包括一个范围,如果这不是问题,那么它将超出您显示的范围,如下所示:

angular.module("app").controller.(testController", ["$scope", function($scope){
     console.log($scope.x)
}])

with: 与:

<div ng-repeat = "x in [1,2]">
        <div ng-include = "'./views/vew.test.html'">
        </div>
</div>

and in vew.test.html: 并在vew.test.html中:

<div ng-controller = "testController">
    {{x}}
</div>

Will wack 1 and 2 on the screen and in the console. 将在屏幕上和控制台中显示1和2。

尝试在控制器中使用$parent.result而不是$scope.result因为ng-include会创建一个自己的新范围,该范围$scope.result是从其父范围继承的

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

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