Angularjsバージョン1.5を使用してフォームの入力を検証しています。Angular ng-requiredがカスタムディレクティブで動作しない
- NG-必要なすべての入力を検証するために使用され、しかし
を必要とそのコンボをレンダリングするカスタムディレクティブで作業していません。コンボは、 'listId'という名前で渡されたパラメータに基づいて項目を取得します。次に、ng-repeatを使用して 'lookupItems'を反復処理します。 ngModelのように何かが欠けていると思います。なぜ、どのように実装するのですか?
コンボディレクティブ:
app.directive('combo', function($http) {
return {
restrict: 'AE',
template: '<div class="input-group"> <select ng-model="selectedItem">' +
'<option ng-repeat="option in lookupItems" value={{option.ListValueID}}>{{option.Translation.Value}}</option></select>' +
' {{selectedItem}} </div>',
replace: true,
scope: {
listId: '=',
defaultItem: '=',
selectedItem: '='
},
controller: function($scope) {
$http({
method: 'GET',
url: '/home/listvalues?listid=' + $scope.listId
}).then(function(response) {
$scope.lookupItems = response.data;
}, function(error) {
alert(error.data);
});
},
link: function(scope, element, attrs) {}
};
});
HTMLビュー:はそのセットの属性」に基づいてブールにNG-必要なレンダリングするコントロールの種類が含まれている属性を反復処理されます。必須です。これは本当です。
<form name="profileAttributesForm" ng-controller="metadataCtrl" class="my-form">
<div ng-repeat="a in attributes">
<div ng-if="a.DataType == 1">
<input type="text" name="attribute_{{$index}}" ng-model="a.Value" ng-required="a.Required" />
<span ng-show="profileAttributesForm['attribute_{{$index}}'].$invalid">Enter a Text</span> text : {{a.Value}}
</div>
<div ng-if="a.DataType == 4">
<div combo list-id="a.LookUpList" name="attribute_{{$index}}" selected-item="a.Value" ng-required="a.Required"></div>
<span ng-show="profileAttributesForm['attribute_{{$index}}'].$invalid">lookup Required</span> Value from lookup: {{a.Value}}
</div>
</div>
</form>
私は、例示の目的のためにそれを提供してい形で反復処理された属性のサンプル($ scope.attributes)、:
[{
"AttributeID": 1,
"DataType": 4,
"NodeID": 0,
"Name": "Name",
"Description": null,
"LookUpList": 1,
"SortAscending": false,
"Required": true,
"DefaultValue": "1",
"Order": 1,
"Value": ""
}, {
"AttributeID": 3,
"DataType": 1,
"NodeID": 0,
"Name": "Job Title",
"Description": null,
"LookUpList": 0,
"SortAscending": false,
"Required": true,
"DefaultValue": null,
"Order": 2,
"Value": ""
}, {
"AttributeID": 4,
"DataType": 1,
"NodeID": 0,
"Name": "Email",
"Description": null,
"LookUpList": 0,
"SortAscending": false,
"Required": true,
"DefaultValue": null,
"Order": 3,
"Value": ""
}]
ありがとう@Cosmin。複雑なソリューションのリンクや例を提供できますか?それを実装する方法については詳しく説明していますか? – Coding
@ h.salman私は私の答えを編集し、あなたを助けるかもしれない記事の束を加えました。 –