Angularjs 以编程方式取消选中角度边界复选框
我试图取消选中使用ng模型绑定到范围变量的复选框。基本上,下面的内容在2秒后不会取消选中 htmlAngularjs 以编程方式取消选中角度边界复选框,angularjs,Angularjs,我试图取消选中使用ng模型绑定到范围变量的复选框。基本上,下面的内容在2秒后不会取消选中 html 尝试使用angular服务,而不是javascript的设置超时 因为如果在angularjs中使用setTimeout,则需要使用$scope.$apply()来确保作用域中的更改。选中此项 但是将为您完成这项工作 像这样 $timeout(function(){ $scope.checked = false; alert('should be unchecked!'); },2
尝试使用angular服务,而不是javascript的设置超时 因为如果在
angularjs
中使用setTimeout,则需要使用$scope.$apply()
来确保作用域中的更改。选中此项
但是将为您完成这项工作
像这样
$timeout(function(){
$scope.checked = false;
alert('should be unchecked!');
},2000);
您需要将选中的变量设置为所选属性
<div ng-controller="AppCtrl">
<input type="checkbox" selected="{{checked}}">
</div>
这项工作对我来说很好!
HTML文件
<input type="checkbox" [(ngModel)]="checked" [checked]="checked" (change)="onChange(checked? 'block':'none')">
选中=在.ts文件上创建的变量
onChange=在.ts文件上创建的一个函数,用于将div的状态更改为display:none | block
<div ng-controller="AppCtrl">
<input type="checkbox" selected="{{checked}}">
</div>
<input type="checkbox" [(ngModel)]="checked" [checked]="checked" (change)="onChange(checked? 'block':'none')">
export class TopBarComponent implements OnInit {
checked: boolean;
constructor(public dados: DadosService) { }
ngOnInit() {
if(this.dados.meusChamados == 'block')
this.checked = true;
else
this.checked = false;
}
onChange(event : any){
this.dados.meusChamados = event;
}
}