Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在angularjs中使用$compile创建邮件模板_Angularjs_Templates_Compilation - Fatal编程技术网

在angularjs中使用$compile创建邮件模板

在angularjs中使用$compile创建邮件模板,angularjs,templates,compilation,Angularjs,Templates,Compilation,我有一个很大的html文件,里面有一些css。此html在填写表单后形成后续电子邮件的正文。现在它的实现方式是通过字符串连接,这是我不喜欢的。新的html电子邮件正文也更大。我想我可以用angulars$编译函数来完成,但我不能让它正常工作 这是最新的。我已经试过了(这是plunk的当前状态),也试过了 完成的和“插入的”(不管是什么意思)字符串是发送实际电子邮件的服务器REST调用的参数。因此,我希望将模板的创建保留在一个用户可以执行的函数中,例如ng click。这就是我的意思: compo

我有一个很大的html文件,里面有一些css。此html在填写表单后形成后续电子邮件的正文。现在它的实现方式是通过字符串连接,这是我不喜欢的。新的html电子邮件正文也更大。我想我可以用angulars$编译函数来完成,但我不能让它正常工作

这是最新的。我已经试过了(这是plunk的当前状态),也试过了

完成的和“插入的”(不管是什么意思)字符串是发送实际电子邮件的服务器REST调用的参数。因此,我希望将模板的创建保留在一个用户可以执行的函数中,例如ng click。这就是我的意思:

composer.$inject = ["$http", "$compile", "$scope", "$templateRequest", "$timeout"];
         function composer($http, $compile, $scope, $templateRequest, $timeout) {

           $scope.person = {
             firstname:"jonny",
             lastname:"Bravo"
           };

           compose();


          function compose() {
            $templateRequest("template.html").then(function(html){
              var template = angular.element(html);
              console.log(template);
              $compile(template)($scope);
              console.log(template);

              $timeout(function() {
                $scope.htmlitem = template.html();
              });

           });
          }

我真想知道为什么它不起作用。

在获取
template.html()之前,您需要将
编译的结果
附加到
文档

已修复

通过使用
指令
来编译模板,并将值提供回控制器,这是一个小技巧。希望它的工作,任何问题让我知道plz

.directive("htmlBuilder", htmlBuilder);

htmlBuilder.$inject = ["$http", "$compile", "$templateRequest", "$timeout"];

  function htmlBuilder($http, $compile, $templateRequest, $timeout) {

    return {
      restrict: 'EA',
      require: 'ngController',
      link: function(scope, $elements, attrs, ctrl) {

        $templateRequest("template.html").then(function(html) {
          var template = angular.element(html);
          $compile(template)(scope);

          //Apend to document before get innerHTML
          $elements.append(template);

          $timeout(function() {
            var res = $elements.html();
            ctrl.setResult(res);
          });
        });
      }
    }


  }

非常感谢你!我还有另一个问题:模型上的更改不会反映在已编译的html中。我应该在最初的问题中更好地解释这一点。我已经更新了plunker以更好地反映这一点。然而,你的答案确实回答了我的问题(我真的不知道我应该附加模板),所以这是唯一公平的,给你的信用。谢谢选中。@ocket san,不客气,修复在编译的html中观看和反映的问题。使用指令的隔离作用域时,您可以通过
scope.$parent
访问控制器的
$scope
。您是我的英雄!如果可以,我会再次接受:-)。我也看到我犯了一些愚蠢的错误。。。非常感谢您的快速回复和回答!