在角度中,为什么我的 ng-show 不更新以反映当前范围

In angular, why are my ng-shows not updating to reflect the current scope?

本文关键字:更新 范围 ng-show 我的 为什么      更新时间:2023-09-26

我在另一个ng-repeat的内部有一个ng-show。该ng-show内部有一个按钮。当我单击该按钮时,我的范围已正确更新,但在我刷新页面之前,ng-show 不会更改。

模板:

<div ng-repeat="quest in quests">
  <p ng-show="{{quest.progress === 0}}">You and goat have found a castle.</p>
  <p ng-repeat="step in quest._steps">
  <span ng-show="{{quest.progress === step.order}}">You are here</span>
  <span ng-show="{{quest.progress + 1 === step.order}}">Goat Says: {{step.flavorText}} {{step.description}}<button ng-click="nextStep(quest)">Step Complete!</button>    
</span>
</p>

控制器:

$scope.nextStep = function(currentQuest, i) {
  currentQuest.progress++;
  console.log('$scope.quests', $scope.quests);
  questService.editQuest(currentQuest._id, currentQuest)
  .then(function(response) {
  });
};

所以现在,当我点击时,我的数据库正在正确更新,并且 $scope.quests 的控制台.log显示了一个正确更新的任务对象(我真的不明白,因为我正在增加 currentQuest.progress 而不是 $scope.quests[$index] 或其他什么,但既然范围已更新,我不在乎),那么为什么没有显示和隐藏正确的 ng-show??

因为您正在使用插值大括号{{}}... ng-show直接读取表达式。许多核心指令都是相同的。

内插大括号用于将文本打印到 dom 中

改变

ng-show="{{quest.progress === 0}}"

ng-show="quest.progress === 0"