Javascript AngularJS ng如果变量只工作一次
我正在维护一段时间前编写的代码,我没有AngularJS方面的经验。我必须在触发HTTP请求时打开微调器,在收到HTTP响应时关闭微调器 微调器由Javascript AngularJS ng如果变量只工作一次,javascript,angularjs,angularjs-ng-if,Javascript,Angularjs,Angularjs Ng If,我正在维护一段时间前编写的代码,我没有AngularJS方面的经验。我必须在触发HTTP请求时打开微调器,在收到HTTP响应时关闭微调器 微调器由ng if和$scope.authenticating变量控制 当前,加载页面时,微调器打开(对应于$scope.authentication===false的初始状态) 用户切换开关,HTTP正确启动,$scope.authentication设置为true,微调器正确显示 当HTTP响应返回时,控制台日志向我显示响应的内容,一切正常,然后$scop
ng if
和$scope.authenticating
变量控制
当前,加载页面时,微调器打开(对应于$scope.authentication===false的初始状态)
用户切换开关,HTTP正确启动,$scope.authentication
设置为true
,微调器正确显示
当HTTP响应返回时,控制台日志向我显示响应的内容,一切正常,然后$scope.authenticating
被设置为false
,但是:微调器仍然存在并且没有消失
感谢您的帮助
下面是代码
控制员:
$scope.authenticating = false
$scope.myfunction = async function (check, change, user) {
if (someCheck(user)) {
$scope.modified = true;
...
$scope.authenticating = true
let res = await AppUserResource.patch(..., ...).$promise;
console.log('res: ' + JSON.stringify(res))
$scope.authenticating = false
}
}
相关HTML代码:
<div style="margin-top:13px">
<div ng-if="!authenticating">
<label class="switch">
<input id="switch" ng-change="bypassApps(disabledAppsCheckbox, true, user)" ng-model="disabledAppsCheckbox"
type="checkbox">
<div class="slider round">
<div class="slider-text pull-left text-green-table">{{'ON' | translate}}</div>
<div class="slider-text pull-right text-red-table">{{'OFF' | translate}}</div>
</div>
</label>
</div>
<div ng-if="authenticating">
<i class="fa fa-spinner fa-spin"style="font-size:270px;margin-bottom:5%; margin-top:5%;"></i>
</div>
</div>
{{'ON'| translate}}
{{'OFF'| translate}}
当使用async/await模式时,它位于angularJS摘要循环之外。因此,您必须调用
$scope.$apply()
你能在模板authenticating
中验证状态是否正确吗?类似于{{authenticating}我做了一个检查,我在HTML代码中放入了{{authenticating},并且在HTTP响应后打印了相同的变量。我注意到在HTML{}HTTP启动时它会正确更改,但在响应后我仍然看到true。但是,在console.log中,验证HTTP响应后更改为false。HTML和controllerOkay之间存在不一致,我猜这与async/await有关。您已尝试执行$scope.$apply()
在您的等待
呼叫后?这应该可以修复$scope.$apply();工作正常!如果您想获得应得的分数,请随时做出完整的响应!