この質問は、質問 1 質問 2の前に少なくとも 2 回尋ねられましたが、答えが完全ではなかったと思います。多くの人が ng-repeat で静的な名前を使用しています。これは可能な解決策ですが、入力の名前が必要なため、私の場合は機能しません。コントローラーで how パラメーターを別のディレクティブに渡す場合、ディレクティブを使用することをおdinamicName
勧めしますが、これではエラーを表示する方法がわかりません
以下は私のコード例です
<form name="myForm" class="form-horizontal" role="form" ng-submit="submitForm()">
<div ng-repeat="field in data.fields">
<ng-form name="form">
<!-- Texto plano -->
<div class="col-sm-6">
<input type="{{ field.type }}" data-ng-model="field.data" class="form-control" required dynamic-name="field.name"/>
<span ng-show="form."+{{field.name}}+".$touched && form."+{{field.name}}+".$error.required" class="help-block with-errors" >Required!</span>
</div>
</div>
</ng-form>
</div>
</form>
ディレクティブ dynamicName:
angular.module('formModule')
.directive('dynamicName',
function dynamicNameDirective ($compile, $parse) {
return {
restrict: 'A',
terminal: true,
priority: 100000,
link: function(scope, elem) {
var name = $parse(elem.attr('dynamic-name'))(scope);
// $interpolate() will support things like 'skill'+skill.id where parse will not
elem.removeAttr('dynamic-name');
elem.attr('name', name);
$compile(elem)(scope);
}
};
});
このコードはスパムメッセージを表示しません
ありがとう