[英]Angular.js - View doesn't update when changing a scope variable in resize event
[英]Angular.js view doesn't update when nested $scope array is updated
我正在嘗試在添加注釋時使angular.js視圖更新。 我的代碼如下:
<div class="comment clearfix" data-ng-repeat="comment in currentItem.comments" data-ng-class="{bubble: $first}" data-ng-instant>
<div>
<p>
<span class="username">{{comment.user}}</span> {{comment.message}}
</p>
<p class="time">
10 minutes ago
</p>
</div>
</div>
<div class="comment reply">
<div class="row-fluid">
<div class="span1">
<img src="assets/img/samples/user2.jpg" alt="user2" />
</div>
<div class="span11">
<textarea class="input-block-level addComment" type="text" placeholder="Reply…"></textarea>
</div>
</div>
</div>
范圍在輸入時更新:
$('.addComment').keypress(function(e) {
if(e.which == 10 || e.which == 13) {
$scope.currentItem.comments.push({
"user": "user3",
"message": $(this).val()
});
console.debug("currentItem", $scope.currentItem);
}
});
調試$ scope.currentItem顯示已添加注釋,但視圖不顯示新注釋。 我懷疑$ scope僅在第一級被監視,這就是視圖不更新的原因。 那是這樣嗎? 如果是這樣,我該如何解決?
解決方案:正如Ajay在下面的回答中建議的那樣,我將數組推送到apply函數中,如下所示:
var el=$(this);
$scope.$apply(function () {
$scope.currentChallenge.comments.push({
"user": $scope.currentUser,
"message": el.val()
});
});
修改代碼以包含在范圍內。$ apply因為你要修改角度范圍之外的屬性你必須使用范圍。$ apply()來監視值
我必須將我的表格放在與ng-repeat相同的div控制器中。 我有兩個獨立的div控制器。
使用$ scope。$ parent.arrayObjet來修改父變量而不是$ scope.arryaObject。 它適用於我的情況。
<div class="col-sm-12 col-lg-12">
<div class="outer-container">
<div class="inner-container">
<div class="table-header">
<form id="teamForm" name="teamForm">
<table class="table table-bordered">
<thead>
<!-- Grid header -->
<tbody data-ng-if="(allTeamMembers.length==0)">
<tr>
<td class="text-center height_33_p" colspan="15"> {{noResultFound}} </td>
</tr>
</tbody>
<!-- Existing team member -->
<tbody data-ng-repeat="tm in teammemberDetailsArryobject|orderBy:orderByField:reverseSort" data-ng-form="innerForm_$index" data-ng-class="{'ng-submitted':innerForm_$index.$submitted}">
<tr></tr>
</tbody>
在代碼中我刪除一條記錄並以編程方式添加新行,但所有行都顯示在ng-repeat行中(包括已刪除)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.