I'm trying to require the ngForm associated to each iteration of ngRepeat within my directive. I've tried to use require: 'form' but it comes back as undefined, also tried '^form', but it gives the mainForm outside the ngRepeat. Is there a special way like require: 'ngForm' that will give me the controller of the ngForm on the first element of my form-view directive?
Parent directive template outside of form-view directive
... // 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>
Form-view directive template
<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>
Form-view directive
.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
}
};
}]);
You can't "require" things that are inside your directive's element, only on it or above it.
One way to do this would be to make your own ngForm directive that requires your formView and then talks to it. It will live along the existing ngForm.
Check out the implementation for ngForm and ngModel for a real-world example - ngForm knows about the ngModels inside it but it's not because the form finds the models, the models let the form know of their existence.