5

I am building a generic code generator using Angular

I have dynamic data structures & templates that are designed to build different types of code. HTML, C#, SQL, Javascript, CSS, Ruby.

enter image description here

I have gotten the $compile function to work as part of a directive and it injects elements in the Angular page fine.

What I really need though is for the generation to output a simple string.

Every article I read talks about taking the compiled output and injecting into the angular pipeline which is not my requirement.

Does anyone know who I can extract a simple string out of a compiled template which has been merged with my scope data?

angular.module('workibleServerApp')
  .directive('generateDynamicTemplate', ['$compile', function ($compile) {
      return {
          scope: true,
          data: '=',
          rawTemplate: '=',
          restrict: 'E',
          link: function (scope, element, attrs) {

              scope.$watchGroup([attrs.data, attrs.rawTemplate], function (newValues) {

                  scope.data = newValues[0];
                  var rawTemplate = newValues[1];

                  element.html('');
                  //element.html('<textarea id="outputRaw" name="outputRaw" class="form-control" rows="12">');

                  if (!_.isEmpty(scope.data) && !_.isEmpty(rawTemplate)) {
                      var tl = angular.element(rawTemplate);
                      var compiled = $compile(tl)(scope);
                      element.append(compiled);

                      scope.convertToString = compiled;
                  }
                  //element.append('</textarea>');
              });


          }
      };
  }]);

convertToString does not get populated

              <div class="col-md-6">
                    <h6><strong>Output</strong></h6>
                    <!-- This renders output from the directive, all GOOD -->
                    <generate-dynamic-template data="model.richData" raw-template="model.template"></generate-dynamic-template>

<!-- Neither of these work, This Does not work -->
<pre>
{{convertToString}}
</pre>
                    <textarea>{{convertToString}}</textarea>

                    <!--<textarea id="output" name="output" ng-model="model.output" rows="20" placeholder="Output" class="form-control input-sm"></textarea>-->
                </div>
David Cruwys
  • 6,262
  • 12
  • 45
  • 91
  • 3
    Please try with serialized html using outerHTML and assign to string. scope.convertToString = compiled[0].outerHTML; – Bharat Aug 13 '15 at 06:40
  • I ended up using Handlebars as the template engine – David Cruwys Mar 25 '16 at 02:35
  • Possible duplicate of [Angularjs use custom interpolation symbols for scope](http://stackoverflow.com/questions/18410289/angularjs-use-custom-interpolation-symbols-for-scope) – Paul Sweatte Feb 08 '17 at 16:53

0 Answers0