我试图在我的指令中要求与 ngRepeat 的每次迭代相关联的 ngForm。我尝试使用require:'form',但它以未定义的形式返回,也尝试了'^form',但它在ngRepeat之外提供了mainForm。有没有像 require: 'ngForm' 这样的特殊方式,它会给我表单视图指令的第一个元素上的 ngForm 控制器?
表单视图指令之外的父指令模板
... // some other markup
<form name="mainForm">
... // some other markup
// Vertically stacked tabs directive
// Creates left/right cols, left=stacked tabs, right=form associated content using transclude
<pills>
// Pill pane content with title attribute passed up to pills during compile for stacked tabs
<pill ng-repeat="form in formData.documents"
title="{{form.name}}">
// Generated form content for each pill pane
<form-view form="form"
model="formModel"></form-view>
</pill>
</pills>
... // some other markup
</form>
表单视图指令模板
<fieldset ng-form="form.id"> // want reference to this controller using require
<h3 ng-bind="form.name"></h3>
<div ng-repeat="field in form.mapping.fields"
... // bunch of generated fields
</div>
</fieldset>
表单视图指令
.directive('formView', [function() {
return {
restrict: 'E',
templateUrl: '/app/components/form-view/formview.html',
scope: {
form: '=',
model: '='
},
require: ['^tabs', '^pills', '^form'], // also tried 'form', '?form', 'ngForm', 'ngform'
link: function( $scope, $element, $attrs, ctrls ) {
$scope.tabsCtrl = ctrls[0]; // got tabs
$scope.pillsCtrl = ctrls[1]; // got pills
$scope.formCtrl = ctrls[2]; // always undefined or grabs mainForm
}
};
}]);