2016-04-05 14 views
1

Angularでエラー検証を次のように実行しています。AngularJSでエラー検証が機能しない

<select ng-model = "$parent.color" class = "form-control" 
    ng-options = "color as color for color in $parent.colors" required> 
    <option value="">Choose an option</option> 
</select> 

<span ng-show="serviceForm.$parent.color.$error.required"> My custom error message </span> 

エラー確認メッセージは表示されません。これを修正するには?

答えて

2

ngRequireモジュールを追加しましたか?

ngRequire

<script> 
angular.module('ngRequiredExample', []) 
.controller('ExampleController', ['$scope', function($scope) { 
    $scope.required = true; 
}]); 
</script> 
<div ng-controller="ExampleController"> 
<form name="form"> 
<label for="required">Toggle required: </label> 
<input type="checkbox" ng-model="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="required" /> <br> 
<hr> 
required error set? = <code>{{form.input.$error.required}}</code><br> 
model = <code>{{model}}</code> 

それが動作する場合は、これを試してみてください。

0

コードが正しく動作するようにすることができます。

<form name="form1"> 
    <select name="select" ng-model = "$parent.color" class = "form-control" 
     ng-options = "color as color for color in $parent.colors" required> 
     <option value="">Choose an option</option>  
    </select> 
    <span ng-show="form1.select.$error.required"> My custom error message </span> 
</form> 

あなたはFormControllerを流していません。これを表示するlink

+0

メッセージが送信時にのみ表示される方法はありますか?今すぐ表示します –

+0

はい、サブミットのフラグとしてスコープ変数を使用できます。サブミットが呼び出されたときにそれを発生させます。 &&演算子とともにng-show条件でも使用します。 Ex $ scope.submitted – rahulgarg

関連する問題