1.自定义指令
.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); }); } }; } ])
2.引入指令
<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>
时间: 2024-10-13 14:37:51