I created a custom directive in angular so that I can disappear the form to submit and replace it with the template using a special message. The desired workflow is as follows:
- The user fills out the form and clicks the submit button.
- The controller updates the model with a new object and throws an event with "formSubmitted" with some arguments.
- The directive listens for the event and fades out the form.
- The directive loads a partial html filled with arguments from the event.
I allowed the first 3 steps, but I could not get around the last step (I do not want to hard-code html as strings, I want to pull it from another file, if possible).
How can I do that?
Edit: some sample code (simplified):
This is the form:
<section class="hero-unit {{containerClass}}" tk-jq-replace-children> <h2>{{formTitle}}</h2> <form class="form-search" name="solform" novalidate> <fieldset> ...
This is the controller:
if( formWasSavedOk ){ $scope.formSubmittedMsg = msg; //Here emits the custom event $scope.$emit( 'solicitud:formSubmitted' ); }
This is the directive:
.directive( 'tkJqReplaceChildren', function( $compile ){ return { link: function( $scope, $iElement/*, $iAttrs*/ ){ //The directive listens for the event $scope.$on( 'solicitud:formSubmitted', function( /*event*/ ){ $iElement .children() .fadeOut(1000) .promise() .done(function(){ var $detachedElments = $(this).detach(); //The html is compiled and added to the DOM $iElement.html( $compile( "<h2>{{formSubmittedMsg}}</h2>" )($scope) ); $scope.$apply(); }); }); } }; });
<h2>{{formSubmittedMsg}}</h2> is the code I want to extract from the application /partials/create/createdOk.html (this is more than just a header, so I want to load it from a file)
source share