I am building a huge form that calls various directives to build a complete form. The Main Page calling the Form Builder passes the ng-model data like this:
<div form-builder form-data=“formData”></div>
Then the Form Builder Page calls various child directive to build various sections of the Form:
FormBuilder.html:
<div form-fields></div>
<div photo-fields></div>
<div video-fields></div>
.. etc.. etc...
When using $scope
in controller, I had no problem accessing the $scope
in the child directives like this:
function formBuilder() {
return {
restrict: 'A',
replace: true,
scope: {
formData: '='
},
templateUrl: 'FormBuilder.html',
controller: function($scope) {
$scope.formSubmit = function() {
// Submits the formData.formFields and formData.photoFields
// to the server
// The data for these objects are created through
// the child directives below
}
}
}
}
function formFields() {
return {
restrict: 'A',
replace: true,
templateUrl: 'FormFields.html',
controller: function($scope) {
console.log($scope.formData.formFields);
}
}
}
function photoFields() {
return {
restrict: 'A',
replace: true,
templateUrl: 'PhotoFields.html',
controller: function($scope) {
console.log($scope.formData.photoFields);
}
}
}
... etc..
But ever since I got rid of the $scope
and started using ControllerAs
, I am having all sorts of trouble accessing 2 way binding with the Parent - Child Controllers.
function formBuilder() {
return {
restrict: 'A',
replace: true,
scope: {
formData: '='
},
templateUrl: 'FormBuilder.html',
controller: function() {
var vm = this;
console.log(vm.formData); // Its fine here
vm.formSubmit = function() {
// I cannot change formData.formFields and formData.photoFields
// from Child Directive "Controllers"
}
},
controllerAs: ‘fb’,
bindToController: true
}
}
function formFields() {
return {
restrict: 'A',
replace: true,
templateUrl: 'FormFields.html',
controller: function() {
var vm = this;
console.log(vm.formData.formFields);
// No way to access 2 way binding with this Object!!!
}
}
}
function photoFields() {
return {
restrict: 'A',
replace: true,
templateUrl: 'PhotoFields.html',
controller: function() {
var vm = this;
console.log(vm.formData.photoFields);
// No way to access 2 way binding with this Object!!!
}
}
}
Whatever I try, I am reaching a road block. Things I have tried are:
- Isolated Scopes: I tried passing
formData.formFields
andformData.photoFields
as isolated scopes to the child directive, but I then end up getting the$compile: MultiDir
error due to nested isolated scopes so it is not possible. - If I don’t have
individual directives for each form section and have all of them in
1 directive under
formBuilder
directive, then it becomes a humungous directive. The above is just a sketch but each child directive builds 1 big form put together in the end. So merging them together is really the last resort since it does become hard to maintain and unreadable. - I don’t think there is a way to access
Parent directive’s
ControllerAs
from Child Directive'sController
any other way from what I have seen so far. - If I use the parent’s ControllerAs in
the child directive template’s ng-model like
<input type=“text” ng-model=“fb.formData.formFields.text" />
, that works fine, but I need to access the same from the Child directive’s controller for some processing which I am unable to do. - If I get rid of the
controllerAs
and use the$scope
again, it works like before but I am trying to get rid of the$scope
altogether to prepare myself for future Angular changes.
Since it is an advanced form, I need to have separate directive to handle various form sections and since nested isolated scopes are not allowed since Angular 1.2, it is making it ever harder especially when trying to get rid of $scope
using ControllerAs
.
Can someone guide me what are my options here please? I thank you for reading my long post.