我有以下 Controller 和指令:
app.controller('controlFormulario', ['$scope', function($scope) {
var cf = this;
cf.formulario = [];
cf.formulario.fecha = new Date();
if(cf.formulario.texto != null && cf.formulario.titulo != null){
this.formulario.isDisabled = false;
}
}]);
app.directive('formulario', [function() {
return {
restrict: 'E', // C: class, E: element, M: comments, A: attributes
templateUrl: 'modules/formulario.html',
};
}]);
这是按钮的 DOM 元素,当标题和文本字段中有一些文本时我想要启用该元素:
<div class="form-group" ng-init="formCtrl.formulario.isDisabled = true">
<button class="btn btn-primary" style="height:35px;width:100px;float:right;" id="submit" disabled={{formCtrl.formulario.isDisabled}}>
Enviar
</button>
</div>
目前,启用该按钮的 Controller 功能根本不起作用。我已经在处方集的主 div 中实例化了它,并且它可以与数据绑定(bind)正常工作,但不知何故无法启用该按钮。
我做错了什么?
最佳答案
使用ng-disabled
而不是 disabled
属性。
<button class="btn btn-primary" style="height:35px;width:100px;float:right;" id="submit" ng-disabled="formCtrl.formulario.isDisabled">
来自链接文档:
A special directive is necessary because we cannot use interpolation inside the
disabled
attribute.
此外,使用 $scope
将数据公开给 View ,而不是 this
。 this
refers to the controller, not to the $scope
that the view can see.
关于javascript - 填写表单时将按钮设置为事件状态( Angular ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31809741/