我在角度创建了一个自定义指令,以便我可以在提交时淡出表单并将其替换为带有自定义消息的模板。所需的工作流程如下:
- 用户完成表单并单击提交。
- 控制器使用新对象更新模型并发出带有一些参数的“formSubmitted”事件。
- 该指令监听事件并淡出表单。
- 该指令加载一个部分 html,其中填充了事件中的 args。
我解决了前 3 个步骤,但无法绕过最后一步(我不想将 html 硬编码为字符串,如果可能的话,我想从另一个文件中提取它)。
如何做呢?
编辑:一些示例代码(简化):
这是表格:
<section class="hero-unit {{containerClass}}" tk-jq-replace-children>
    <h2>{{formTitle}}</h2>
    <form class="form-search" name="solform" novalidate>
        <fieldset>
        ...
这是控制器:
if( formWasSavedOk ){
    $scope.formSubmittedMsg = msg;
//Here emits the custom event
    $scope.$emit( 'solicitud:formSubmitted' );
}
这是指令:
.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>是我想从 app/partials/create/createdOk.html 中提取的代码(它不仅仅是一个标题,这就是我想从文件中加载它的原因)