禁用指令

时间:2016-09-25 17:55:01

标签: angularjs

我只想在disableform.$valid时添加课程false。第一个提交按钮(非指令)显然可行,但我不知道如何使指令中的表单状态可见。我不能在指令中硬编码表单名称,它应该可以以多种形式重用..

<form id="tA" name="form" ng-controller="ctrl" ng-submit="form.$valid && save()" novalidate>

    <input type="text" class="form-control" placeholder="title" name="name" ng-model="model.name" required ng-required="true">

    <button class="btn btn-default" ng-class="{'disabled' : !form.$valid}" class="btn btn-default">Submit</button>

<button-raised disabled="!form.$valid">directive Submit</button-raised>
app.directive('buttonRaised', function() {
return {
    restrict: 'E',
    template: '<button class="mdl-button" ng-class="ngClass" ng-transclude></button>',
    scope: {
        ngModel: '='
    },
    transclude: true,
    link: function($scope, el, $attrs,formCtrl) {
        console.log(formCtrl)
        $scope.ngClass = {
           // 'disabled': $scope.$parent.formCtrl.$valid,
        };
    }
};
});

2 个答案:

答案 0 :(得分:1)

执行此操作的一种方法是将布尔值作为指令的参数传递:

String input = ...

String outputString;

if (input.isEmpty()) {
    outputString = "";
} else {
    StringBuilder sb = new StringBuilder().append(input.charAt(0));

    for (int i = 1; i < input.length(); i++) {
        char c = input.charAt(i);
        if (Character.isUpperCase(c)) {
            sb.append(' ').append(Character.toLowerCase(c));
        } else {
            sb.append(c);
        }
    }
    outputString = sb.toString();
}

System.out.println(outputString);

});

答案 1 :(得分:1)

您可以根据此工作this

更改指令和html

指令

 <form id="tA" name="form" ng-controller="ctrl" ng-submit="form.$valid && save()" novalidate>

  <input type="text" class="form-control" placeholder="title" name="name" ng-model="model.name" required ng-required="true">

  <button class="btn btn-default" ng-class="{'disabled' : !form.$valid}" class="btn btn-default">Submit</button>


  // directive taking parameter disable-button
  <button-raised disable-button="!form.$valid">directive Submit</button-raised>

html

{{1}}