We have a page that opens a modal dialog with a form like below. However when we hit the controller that should handle the form action, the form object is undefined and I am too much of an Angular newbie to understand why...
This is the parent page controller holds the function to open the modal dialog:
app.controller('organisationStructureController', ['$scope', ..., '$modal', function ($scope, ..., $modal) {
$scope.openInvitationDialog = function (targetOrganisationId) {
$modal.open({
templateUrl: 'send-invitation.html',
controller: 'sendInvitationController',
resolve: {$targetOrganisationId: function () {
return targetOrganisationId;
}
}
}
);
};
on a page like this:
// inside a loop over organisations
<a ng-click="openInvitationDialog({{organisation.id}})">Invite new member</a>
the invitation-dialog html looks like this:
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<!-- ... -->
</div>
<div class="modal-body">
<form name="invitationForm">
<div class="form-group">
<label for="email" style="color:white;">Email</label>
<input type="email" class="form-control" autocomplete="off" placeholder="New member email" id="email" name="email" ng-model="invitation.email" required="true"/>
<span class="error animated fadeIn" ng-show="invitationForm.email.$dirty && invitationForm.email.$error.required">Please enter an email address!</span>
<span class="error animated fadeIn" ng-show="invitationForm.email.$error.email">Invalid email</span>
</div>
<!-- ... -->
<div class="modal-footer">
<button type="button" class="btn btn-default" ng-click="cancel()">Cancel</button>
<button type="submit" class="btn btn-primary" ng-click="sendInvitation()">Invite</button>
</div>
</form>
</div>
</div>
</div>
The controller that should handle the invitation is somewhere else:
app.controller('sendInvitationController', ['$targetOrganisationId', '$scope', ...,
function ($targetOrganisationId, $scope, ...) {
$scope.invitation = {
// ...
targetOrganisation: {
id: $targetOrganisationId
}
};
$scope.sendInvitation = function () {
// $scope.invitationForm is undefined
if ($scope.invitationForm.$invalid) {
return false;
}
// send the invitation...
};
}]);
So what's the correct way to get the form scope into the controller?
Maybe I need to inject $modal
into the sendInvitationController
and add the sendInvitation
function to it? But when I do that the action never enters the controller. Or do I have to add the function that handles the submit action to $modal.open({ ...
instead of referencing the controller? Though I'd much prefer to have the sendInvitationController in its own file and scope.
Thanks for any help!
EDIT
We found several things that helped us build a workaround and might help someone answer the question itself:
- the
$scope.invitation
object is not undefined in thesendInvitationController
but holds the correct data, while$scope.invitationForm
remains undefined. - inside the send-invitation.html we can access
$scope.invitationForm.$invalid
and do the validation right there:<button type="button" ng-click="sendInvitation()" ng-disabled="invitationForm.$invalid">Invite</button>
So the question is: why does the binding of the invitationForm
object to the $scope
fail on submit while the form model binds correcetly?