[英]Angular Nested Controllers Access ng-repeat data
I have two controllers, one nested inside the other, both using ng-repeat to essentially list arrays of related data. 我有两个控制器,一个嵌套在另一个内,都使用ng-repeat实质上列出了相关数据的数组。 I'd like to access one of the properties in the ng-repeat of the parent controller in the child controller.
我想访问子控制器中父控制器的ng-repeat中的属性之一。 I'm pretty new to Angular and not sure how to get this working or if I'm approaching it the wrong way.
我是Angular的新手,不确定如何使它正常工作,或者我是否以错误的方式进行操作。 Any guidance would be helpful.
任何指导都会有所帮助。
HTML 的HTML
<div class="container" ng-app="myApp">
<div class="task" ng-controller="TaskController as taskCtl" ng-repeat="task in tasks">
{{task.name}}
<ul>
<li ng-controller="AttachmentController as attachmentCtl" ng-repeat="attachment in attachments">{{attachment.name}}</li>
</ul>
</div>
</div>
JS JS
var app = angular.module('myApp', []);
app.controller('TaskController', ['$scope', function ($scope) {
$scope.tasks = [{name:'thing1', id: '123456'}, ... ];
}]);
app.controller('AttachmentController', ['$scope', '$http', function ($scope, $http) {
$scope.attachments = [];
$scope.init = function init() {
$http.get('/api/attachments&task_id=' + **HOW_DO_I_GET_TASK_ID_HERE** )
.then(function(response) {
$scope.attachments = response.data;
});
};
$scope.init();
}]);
I'd like to load the attachments as they relate to the tasks based on the task id for a given iteration through ng-repeat. 我想通过ng-repeat根据给定迭代的任务ID加载与任务相关的附件。 Not sure if I'm going about this the wrong way.
不知道我是否会以错误的方式进行操作。
Thanks 谢谢
Although it would be better to use a ng-repeat with a filter on all the attachments with the given id. 尽管最好对具有给定id的所有附件使用带有过滤器的ng-repeat。 Since now you are calling the
/api/attachments&task_id
for each task iteration. 从现在开始,您将为每个任务迭代调用
/api/attachments&task_id
。
Or to send the list of attachments directly on the /api/tasks
call. 或直接在
/api/tasks
调用中发送附件列表。 Therefor you could loop them instantly when looping the tasks, without the need of fetching them on each iteration. 因此,您可以在循环任务时立即循环执行它们,而无需在每次迭代时都提取它们。
A possible solution according to your code provided: 根据提供的代码,可能的解决方案:
<div class="container" ng-app="myApp">
<div class="task" ng-controller="TaskController as taskCtl" ng-repeat="task in tasks">
{{task.name}}
<ul>
<li ng-controller="AttachmentController as attachmentCtl" ng-repeat="attachment in getAttachments(task.id)">{{attachment.name}}</li>
</ul>
</div>
</div>
app.controller('AttachmentController', ['$scope', '$http', function ($scope, $http) {
$scope.getAttachments = function(id) {
$http.get('/api/attachments&task_id=' + id)
.then(function(response) {
return response.data;
});
};
}]);
Something like this from the child controller should work: HTML: 子控制器中的类似内容应该可以正常工作:HTML:
<div class="container" ng-app="myApp">
<div class="task" ng-controller="TaskController" ng-repeat="task in tasks">
{{task.name}}
<ul>
<li ng-controller="AttachmentController" ng-repeat="attachment in fetchAttachments(task)">{{attachment.name}}</li>
</ul>
</div>
</div>
JS Child Controller: This fetchAttachments will be called for every iteration of the parent ngRepeat. JS子控制器:父ngRepeat的每次迭代都将调用此fetchAttachments。 You will have to "return" the result of the Ajax call to this function for it to work.
您必须将“ Ajax”调用的结果“返回”给该函数才能起作用。
$scope.fetchAttachments = function (task) {
// call ajax
// return the result of ajax
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.