Angularjs - ng-disabled默认值

时间:2015-01-20 09:35:38

标签: angularjs

我有一个具有此属性的按钮:ng-disabled="ProductionBtnDisabled"

当angular呈现html时,ProductionBtnDisabled的值未定义,并且在启动控制器ProductionBtnDisabled后具有正确的值。

首先,ng-disabled未被禁用,因为javascript / angular中的undefined=false。这对我来说是个问题。我希望默认值为true。

有人有任何建议要处理这个问题吗?

使用ng-cloak怎么样?它对我不起作用。在渲染范围之前,我不介意隐藏按钮。

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为ngDisabled没有默认设置。如果您需要全局解决方案,可以尝试使用指令。

.directive('specialDisabled',function() {
    return {
      restrict: 'A',
      scope:{specialDisabled: '='},
      link: function(scope, element, attrs) {
        scope.$watch(function(){return scope.specialDisabled}, function(){
          // set disabled attribute here
          element[0].disabled = !scope.specialDisabled;  
        });
      }
    }
  });

然后,您可以在任何地方调用specialDisabled指令。

<input type="checkbox" ng-model="chk">
<button special-disabled="chk">My Button</button>

http://plnkr.co/edit/BA2ntTrzmItwvEj8UKOc?p=preview