我是一个表单中输入创建动态。 我创造了这个指令的目的:
// Generate inputs on the fly using BE data.
.directive('inputGenerator', ['$compile', function ($compile) {
return {
restrict: 'E',
scope: {
id: '@',
type: '@',
name: '@',
attributes: '=',
ngModel: '=',
ngDisabled: '='
},
link: function (scope, iElement, iAttrs) {
// Get attributes
var id = iAttrs.id,
type = iAttrs.type,
name = iAttrs.name;
scope.ngModel[name] = {};
var extended_attributes = {
"type": type,
"id": id,
"data-ng-model": 'ngModel.' + name + '[\'value\']',
"name": name,
"data-ng-disabled": "ngDisabled"
};
if ( ! scope.attributes) {
scope.attributes = {};
}
// Append extra attributes to the object
angular.extend(scope.attributes, extended_attributes);
// Generate input
var input = '<input ';
angular.forEach(scope.attributes, function (value, key) {
input += key + '="' + value + '" ';
});
input += '/>';
// Compile input element using current scope (directive) variables
var compiledElement = $compile(input)(scope);
// Set the file selected name as the model value
compiledElement.on('change', function () {
if (this.files && this.files[0].name) {
var that = this;
scope.$apply(function () {
scope.ngModel[name] = {};
scope.ngModel[name]['value'] = that.files[0].name;
});
}
});
// Replace directive element with generated input
iElement.replaceWith(compiledElement);
}
};
}]);
这个HTML线将触发指令:
<input-generator data-name="{{ item.name }}" data-ng-model="inputs.sources" data-attributes="item.attrs" data-type="{{ item.type }}" data-id="inputFile_{{ $index }}" data-ng-disabled="inputs.sources[item.name].selected" />
我对角1.4.3运行。
问题
该模型和几乎所有的指令工作正常,但由于某些原因,形式仍然有效时添加的输入是无效的,你可以在此图像中看到。
我已经尝试过:
任何形式的验证作品的角度特性
我调试角度,似乎是附加到表单的输入是来自内部的指令编制的输入不同。
我已经叫formName.$setPristine()
每个输入创建后,但没有奏效。
我无法从该指令访问的形式,但我认为是不是一个好主意,无论是。
我已经包裹着NG-form标签的输入,但没有什么有用的出来这一点。
我试图用指令编译方法,但是这仅仅是触发一次,当应用程序加载时,我已经上改变加载不同的输入选择输入。
任何帮助,非常感谢! :)
谢谢大家反正贡献!