javascript - 切换复选框在 Angular js 中不起作用,它只是一直在炫耀
问题描述
我想根据范围变量显示和关闭。但它总是炫耀,以防打开或选中。在控制台窗口中它的显示被选中,但在切换按钮中它的炫耀
html
<input type="checkbox" disabled ng-checked="{{comp1status}}"
data-toggle="toggle" data-onstyle="outline-success"
data-offstyle="outline-danger"><br />
试过
<input type="checkbox" disabled ng-model="comp2status" data-toggle="toggle"
data-onstyle="outline-success" data-offstyle="outline-danger">
这也行不通
js
$scope.comp1status = response.data.ISCmpr1;
if ($scope.comp1status === "0") {
$scope.comp1status = "checked";
}
else {
$scope.comp1status = "unchecked";
}
解决方案
也许,这段代码会帮助你
class AppController {
constructor($scope, $q) {
this.deferred = $q.defer();
$scope.comp1status = true;
this.getComp2Status()
.then(comp2status => {
$scope.comp2status = comp2status;
});
}
getComp2Status() {
setTimeout(() => {
this.deferred.resolve(true);
}, 2000);
return this.deferred.promise;
}
}
AppController.$inject = ['$scope', '$q'];
angular.module('app', [])
.controller('appController', AppController);
angular.bootstrap(document.body, ['app']);
html,
body {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.app {}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<div ng-controller="appController" class="app">
<div class="form-group form-check">
<input ng-model="comp1status" type="checkbox" class="form-check-input" id="comp1status">
<label class="form-check-label" for="comp1status">Comp1 status</label>
</div>
<div class="form-group form-check">
<input ng-model="comp2status" type="checkbox" class="form-check-input" id="comp2status">
<label class="form-check-label" for="comp2status">Comp2 status async</label>
</div>
</div>
推荐阅读
- javascript - 如何在聚焦文本输入字段的同时更改 div 的背景颜色?
- html - 不需要 - 移动视图中的空白 - 为什么会发生?
- reactjs - React live server 没有显示任何内容,只是显示选项卡继续加载
- python - 我正在尝试在 discord py 中扮演反应角色
- c++ - 在 C++ 中加速嵌套循环
- php - 为什么laravel停止显示更改一段时间后,我必须一次又一次地删除缓存文件,任何解决方案
- r - tidyr::pivot_longer 提出:错误:无法结合`TSPAN6`
和`MT-CO2` - android - 如何删除exoplayer中的当前媒体源?
- asp.net-core - ASP.NET Core 3.1 MVC 身份问题
- git - 为什么 git rebase --continue 显示 COMMIT_EDITMSG.swp 文件已存在的错误?