javascript - 填写表单时将按钮设置为事件状态( Angular )

标签 javascript angularjs

我有以下 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 ,而不是 thisthis refers to the controller, not to the $scope that the view can see.

关于javascript - 填写表单时将按钮设置为事件状态( Angular ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31809741/

相关文章:

javascript - JSON 数据未在 Angular JS 中加载

angularjs - Visual Studio Code 提示 *.d.ts 文件中定义的类型为 "Cannot find namespace"

javascript - (Angularjs) 共享一个 Controller 的 View 之间的数据不同步

javascript - 当 dataType 为 "json"时,成功回调不会运行

javascript - 无法在对象(生成的应用程序 Controller )上找到属性 'link_to'。

javascript - ng-repeat 中的 Angular js 动态绑定(bind)

javascript - SSE 回调后 AngularJS 和处理范围刷新

javascript - AngularJS 如何实现滑动翻页动画?

javascript - 为 Shiny 应用程序添加 'Calculation In Process' 指示器

javascript - 如何在访问选择器后访问 css 选择器属性