2016-07-18 5 views
0

ウェブアプリケーションを作成中です。それは5つの必須入力フィールドを持つフォームを持っています。それは2つのボタンを持っています。 1つは&を提出し、もう1つは後で保存します。 私がsubmitをクリックすると、フォームはユーザが入力したすべての必須フィールド&を保存します。これは私のためにうまくいきます。角型JSを使用して動的にバリデーションを削除する

「後で保存」をクリックすると、最初の入力フィールドだけが必須になります。他のフィールドはすべてオプションとして変更する必要があります。どのように角度jsを使用してこれを達成するには?

+1

"ng-required"を検証用に試してみよう –

+0

ng-requiredの中で関数を呼び出してフラグを使って、それが保存か保存かを判断できます。この場合、あなたは好きなように検証を処理できるはずです。 –

答えて

0

ng-requiredを参照してください。

ngRequired内の角型式がtrueと評価された場合、ディレクティブは要素に必要な属性を設定します。以下

例:

angular 
 
    .module('exampleApp', []) 
 
    .controller('ExampleController', ExampleController); 
 

 
function ExampleController() { 
 
    var vm = this; 
 
    vm.required = false; 
 
}
<!DOCTYPE html> 
 
<html ng-app='exampleApp'> 
 

 
<head> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js"></script> 
 
</head> 
 

 
<body ng-controller="ExampleController as vm"> 
 
    <form name="vm.form"> 
 
    <label for="required">Toggle required:</label> 
 
    <input type="checkbox" ng-model="vm.required" id="required" /> 
 
    <br> 
 
    <label for="input">This input must be filled if `required` is true:</label> 
 
    <input type="text" ng-model="model" id="input" name="input" ng-required="vm.required" /> 
 
    <br> 
 
    <p>required status: {{vm.required}}</p> 
 
    <p>form error: {{vm.form.input.$error}}</p> 
 
    </form> 
 

 
</body> 
 

 
</html>

+0

Scottに感謝します。これを試みます。 – Ramya

+0

コードを使用しようとしました。 – Ramya

+0

は2つのボタンで同じコードを試しました。後で保存するためにクリックすると、必要な値をfalseに変更しようとしました。 Didnt work。あなたは助けてもらえますか? ( 'ngRequiredExample'、[]) .controller( 'ExampleController'、laterSave); 関数laterSave($ scope){ $ scope.required = false; } – Ramya

1

ビュー

<form name="Form" ng-controller="testController"> 
<input name="input" type="text" id="txtName" ng-model="Name" class="form-control" required> 
<select ng-model="state" ng-options="s for s in stateList" id="state" ng-change="stateOnChange()" class="form-control"></select> 
<input name="input" type="text" id="txtstate" ng-model="pincode" class="form-control" required> 
<input name="input" type="text" id="txtplace" ng-model="place" class="form-control" ng-required={{isRequired}}> 
<button type="submit" class="btn btn-success" ng-submit="saveAction();">Save</button> 

角度コントローラ

$scope.isRequired = false; 
$scope.stateOnChange = function() { 
if ($scope.state == "TN") { 
    $scope.isRequired = true; 
} 
else { 
    $scope.isRequired = false; 
}} 
関連する問題