[英]Update parent scope through child scopes in Angular
<div class="full-row" ng-repeat="row in pendingRequests | orderBy: '-modificationDate' | partition:3">
<div class="one-third" ng-repeat="request in row track by request.id">
<div class="incoming_request" ng-class="actionClass(request)">
<div class="request_comments">
<hr>
<p><span>Recipients:</span></p>
<div class="comments">
<p ng-repeat="comment in request.comments track by comment.id" class="dont-break-out">
<span class="author" ng-bind="comment.userName | employeeName">:</span>
{{comment.text}}
<span ng-if="comment.status == State.APPROVED" class="approval success" ng-click="changestatus(comment, request)"><i class="fa fa-check"></i></span>
<span ng-if="comment.status == State.REJECTED" class="approval error" ng-click="changestatus(comment, request)"><i class="fa fa-times"></i></span>
<span ng-if="comment.status == State.PENDING" class="approval" ng-click="changestatus(comment, request)" title="{{showApproveTooltip(comment)?'Click to approve the request on behalf of this user':''}}"><i class="fa fa-clock-o"></i></span>
</p>
</div>
</div>
<div class="request_resolve">
<hr>
<div class="textarea-holder">
<textarea placeholder="Your comment..." ng-model="request.newComment" ng-model-options="{ updateOn: 'default blur'}"></textarea>
</div>
<div class="button-container">
<button ng-click="approve(request);" ng-disabled="request.isProcessing" class="btn btn-primary" am-hide-request-resolve-div>Confirm<i ng-hide="request.isProcessing" class="fa fa-check"></i><span ng-show="request.isProcessing" class="spinner no-hover"><a><i class="fa-li fa fa-spinner fa-spin"></i></a></span></button>
<button ng-click="reject(request);" ng-disabled="request.isProcessing" class="btn btn-default pull-right" am-hide-request-resolve-div>Reject <i class="fa fa-times"></i></button>
</div>
</div>
Here is peace of code. 这是和平的代码。 As You may see there are many ng-repeat
s. 如您所见,有许多ng-repeat
。 My pendingRequests
collection very often is updated from server. 我的pendingRequests
集合经常从服务器更新。 After 3 or more updates when I click on some button nothing is happend on UI. 经过3次或更多次更新后,当我单击某些按钮时,UI上没有任何反应。
Details : 细节 :
On approve click I change status
of one comment
. 批准后,单击“我更改一个comment
status
”。
$scope.approve = function (request) {
var currentUserComment = request.comments.filter(function(comm) {
return comm.userId == user.id && comm.status == "Pending";
})[0];
currentUserComment.status = State.APPROVED; // change comments Status
currentUserComment.text = request.newComment;
delete request.newComment;
if (!currentUserComment) {
request.isProcessing = false;
return;
}
Comments.update(currentUserComment, function() {
// $rootScope.$broadcast('daysUpdated');
});
request.isProcessing = false;
};
This must show this span <span ng-if="comment.status == State.APPROVED" class="approval success" ng-click="changestatus(comment, request)"><i class="fa fa-check"></i></span>
, cause now status is equal to State.APPROVED
. 这必须显示该跨度<span ng-if="comment.status == State.APPROVED" class="approval success" ng-click="changestatus(comment, request)"><i class="fa fa-check"></i></span>
,因为现在状态等于State.APPROVED
。 But nothing happens. 但是什么也没发生。
After some research I think it all because ng-repeat
and collection updates. 经过一些研究,我认为这都是因为ng-repeat
和collection更新。
ng-repeat
creates a child scope for each item, and so the scopes in play might look like this: ng-repeat
为每个项目创建一个子作用域,因此作用域可能如下所示:
bookCtrl scope = { tags: [ 'foo', 'bar', 'baz' ] } bookCtrl scope = {标签:['foo','bar','baz']}
ng-repeat
child scopes: { tag: 'foo' }, { tag: 'bar' }, { tag: 'baz' } ng-repeat
子作用域:{标签:'foo'},{标签:'bar'},{标签:'baz'}
So when I update comment.status
for some request
Angular don't know in what scope it exists. 因此,当我为某些request
更新comment.status
,Angular不知道它在什么范围内存在。
Am I right? 我对吗? And how can I solve my problem (after changing comment status show correct span)? 以及如何解决我的问题(更改评论状态后显示正确的跨度)?
More Simple code : 更简单的代码:
<div ng-repeat="request in requests">
<div ng-repeat="comment in request.comments">
<span ng-if="comment.status == State.APPROVED" class="approval success"><i class="fa fa-check"></i></span>
<span ng-if="comment.status == State.REJECTED" class="approval error"><i class="fa fa-times"></i></span>
<span ng-if="comment.status == State.PENDING" class="approval"><i class="fa fa-clock-o"></i></span>
</div>
<div>
<button ng-click="approve(request)">
Approve
</button>
</div>
</div>
And approve function : 并批准功能:
var user = LoggeInUser(); // some user who is loggedIn now
$scope.approve = function(request){
var currentUserComment = request.comments.filter(function(comm) {
return comm.userId == user.id && comm.status == "Pending";
})[0];
currentUserComment.status = State.APPROVED; // change comments Status
Comments.update(currentUserComment, function() { // send PUT request to API
// $rootScope.$broadcast('daysUpdated');
});
}
You may find your solution in moving the functions to $scope.functions.functionName. 您可以在将函数移至$ scope.functions.functionName中找到解决方案。 I believe from reviewing this that you are running into scoping issues, as you alluded to in your statement. 我相信,从评论中可以看出,您正在遇到范围界定问题,正如您在声明中提到的那样。
JS JS
$scope.functions.approve = function () { ... }
HTML 的HTML
functions.approve(request)
You also might have a look at using controller as, sometimes that can help: 您可能还会看看将controller用作,有时会有所帮助:
https://docs.angularjs.org/api/ng/directive/ngController https://docs.angularjs.org/api/ng/directive/ngController
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.