angularjs - Ng-show 值更改后不更新
问题描述
JS
$scope.getDiffs = function () {
return Module.getDiffs($scope.item.account_id, $scope.item.year)
.then(function (res) {
angular.forEach(res, function (v) {
angular.forEach($scope.months, function (m) {
if (m.month == v.month) {
m.diff = v.diff != 0;
}
});
});
});
};
刀
<ul class="nav nav-tabs mbtm-10">
<li role="presentation" ng-repeat="m in months"
ng-class="{active: item.month == m.month}">
<a href="" ng-click="item.month = m.month;fetchTrx()">
@{{m.text}}
<i ng-show="m.diff != null" class="fa fa-circle"
ng-class="{'text-success': !m.diff, 'text-danger': m.diff}">
</i>
</a>
</li>
</ul>
如果值为 ,我的代码将在水龙头界面上显示绿色圆圈m.diff != null
。但是,如果值为 ,它将不会更新m.diff == null
。一旦点击显示绿色圆圈,则无论值多少,它都会显示永远的绿色圆圈。
解决方案
使用angular.copy
:
$scope.getDiffs = function () {
return Module.getDiffs($scope.item.account_id, $scope.item.year)
.then(function (res) {
angular.forEach(res, function (v) {
angular.forEach($scope.months, function (m,i,arr) {
if (m.month == v.month) {
m.diff = v.diff != 0;
arr[i] = angular.copy(m);
}
});
});
});
};
推荐阅读
- android-studio - 必须声明元素清单
- eloquent - laravel 5.6中的sql插入数组到字符串转换错误
- javascript - 在 javascript 中是否可以在 if 语句或 switch 中使用包含?
- regex - 正则表达式查找所有类定义?
- python - 如何使用 Python 确定图像中有多少条不同的曲线?
- linux - 如何将参数列表存储在 bash 的变量中?
- oracle - Oracle Rest – 按列过滤(垂直过滤)
- statistics - 多元线性回归:显着的方差分析但没有显着的系数预测因子?
- javascript - 错误类型错误:“this.posts 未定义” - 在角度类型脚本类中
- sql-server - 如何将存储在列中的 JSON 数组中的每个元素的行拆分为一行?