.directive("dyName", [
function() {
return {
require: "ngModel",
link: function(scope, elm, iAttrs, ngModelCtr) {
ngModelCtr.$name = scope.$eval(iAttrs.dyName)
var formController = elm.controller(‘form‘) || {
$addControl: angular.noop
};
formController.$addControl(ngModelCtr);
scope.$on(‘$destroy‘, function() {
formController.$removeControl(ngModelCtr);
});
}
};
}
])
<div ng-repeat="item in demo.fields">
<div class="control-group">
<label class="control-label"> : </label>
<div class="controls">
<input type="number" dy-name="item.field" ng-model="demo.data[item.field]" min="10" max="500" ng-required="true"/>
</div>
</div>
</div>
AngularJS 进阶(二) 解决form验证时ng-repeat不能先解析name值问题
原文:http://my.oschina.net/sourcecoding/blog/306856