AngularJS-如何仅对活动字段使用ng必需

时间:2018-10-04 22:57:35

标签: angularjs node.js angularjs-directive

  • 我有一个应用程序,其中有切换按钮,当单击该按钮时,它会激活用于用户输入的字段。
  • 字段在一页上,按钮这样调用:

<span ng-include="'partials/triggers/toggleButton.html'"></span>
<a ng-click="toggle()">
<span class="glyphicon" ng-class="{'glyphicon-ban-circle': t.active, 'glyphicon-ok-circle': !t.active}"></span>
</a>
<div ng-controller="TriggerController" ng-init="init(relay, 'inactiveFor')">
<fieldset class="form-group sentinel-line" ng-disabled="!t.active">
<div class="form-group" bs-has-error>
<input type="text" name="timerEveryMinute" class="form-control" ng-model="t.on.val" ng-pattern="/^(((([1-9])|([1-9][0-9])|([1-9][0-9][0-9])|([1-9][0-9][0-9][0-9]))))$/"/> minutes
<span ng-show="form.timerEveryMinute.$invalid && showInvalids" class="formError">Incorrect value</span>
</div>
<span style="color:#B40404" class="help-block" ng-show="!form.timerEveryMinute.$valid">
Invalid! Must be number with max four digits, without decimals.
<br>Correct: "2" or "23" or "2323" . Incorrect: "2,32" or "23.2" or "232323" .
</span>
<div class="form-group" bs-has-error>
<input type="text" name="timerForMinute" class="form-control" ng-model="t.off.val" ng-pattern="/^(((([1-9])|([1-9][0-9])|([1-9][0-9][0-9])|([1-9][0-9][0-9][0-9]))))$/"/> minutes...
<span ng-show="form.timerForMinute.$invalid && showInvalids" class="formError">Incorrect value</span>
</div>
<span style="color:#B40404" class="help-block" ng-show="!form.timerForMinute.$valid">
Invalid! Must be number with max four digits, without decimals.`
`<br>Correct: "2" or "23" or "2323" . Incorrect: "2,32" or "23.2" or "232323" .
</span>
<span ng-include="'partials/triggers/toggleButton.html'"></span>
</fieldset>
</div>

  • 仅当通过切换按钮激活字段时,如何使它们成为必填字段(非空)?正则表达式在这里不起作用,仅当用户进行一些输入时才起作用。

整个代码在这里: https://github.com/romanicak/growduino-client/tree/master/src

1 个答案:

答案 0 :(得分:0)

在控制器中使用变量来跟踪是否需要这些字段:

bool size_equal_to() { ... }
bool size_greater_to() { ... }
bool size_less_than() { ... }
bool type_is_file() { ... }
bool type_is_dir() { ... }
bool mtime_plus() { ... }

单击按钮时,切换该变量

$scope.isRequired = false;

在您的输入中,将需求绑定到该变量:

<button ng-click="isRequired = !isRequired">

就是这样。即使您未在控制器中定义变量,它也将起作用。

Working JSFiddle