0
まずはAngularの新機能です。 HTMLのフォームグループdivで使用されているディレクティブを作成しました。これには必要な入力が含まれています。このディレクティブの理想は、div全体がスコープ値に基づいて表示されるかどうかをチェックすることです。カスタムディレクティブでフォームに必要なフィールドを動的に設定する
視覚的に、この値がfalseにNG-IFディレクティブを設定するときに、フォームが「doesnの際に必要な値がが欠落しているとして、フォームの送信はまだエラーを与える期待通りに動作しますが、...もはやその入力を含んでいません。
これは私のコードです:
// My Directive
app.directive("showCountry", function ($compile) {
var linkFunction = function (scope, element, attributes) {
var testingVariable = scope.testingVariable;
if (testingVariable) {
switch (testingVariable.toLowerCase()) {
case "A":
// Do nothing, keep the div visible.
break;
default:
// Hide the entire div.
attributes.$set("ngIf", false);
\t \t \t \t \t compile(element)(scope);
break;
}
}
};
return {
restrict: "A",
link: linkFunction
};
});
<!-- My HTML with required field using my directive -->
<div class="form-group" show-country>
\t <label class="col-lg-6 col-md-6 control-label">
\t \t Country
\t </label>
\t <div class="col-lg-6 col-md-6">
\t \t <input name="country" type="text" placeholder="Country" ng-model="country" ng-required="true"/>
\t </div>
</div>
は君たちをありがとうございます。
は、ご返信いただきありがとうございます。必要なフィールドが複数ある場合はどうなりますか?別の検証変数の作成を避けるために私は何ができますか? –
同じ条件の異なるフィールドで同じ変数を使用することができます。 Howzあなたのシナリオ? – Sravan
私はこのコードを持っていますが、2つの必須フィールドがあります。同じ変数を使用すると、入力の1つが表示されません。 –