0

I'm relatively new to Angular and am having difficulty getting my directive output to render as HTML (vice a literal string).

Directive in HTML: <div odometer-display="{{bigNumber}}"></div>

The Directive:

app.directive("odometerDisplay",function($compile){//distribute bigNumber digits into individual table cells
var linkFunction = function(scope, element, attributes){
 scope.bigNum = attributes["odometerDisplay"];
 var oTbl="<table><tr>";//start the table

 for(var a=0;a<scope.bigNum.length;a++){//build table cells with digits as content
  oTbl+= scope.bigNum[a]!== "" ? "<td class=\"oCell\">"+scope.bigNum[a]+"</td>":'';
 }

 oTbl+="</tr></table>";//finish the table
 var markUp=$compile(scope)(oTbl);//compile
 element.append(markUp);//append to DOM
}
 return {
    restrict:"A",
    template:'<div>{{oTbl}}</div>',
    link:linkFunction
  }
});

Essentially, linkFunction takes scope.bigNumber and writes each digit into a table cell (defined in variable oTbl). bigNumber is available in '$scope' and in the directive's 'scope' (that's not a problem).

The output (oTbl): <table><tr><td class="oCell">4</td><td class="oCell">8</td><td class="oCell">2</td><td class="oCell">7</td><td class="oCell">2</td></tr></table>

The output is just as it should be (a table with each cell hosting one of bigNumber's digits -- very simple).

The problem is that it does not render as HTML -- it simply renders the literal string defining the table and cells (even after an attempt to '$compile' and '$append').

This is merely the last attempt I've made at getting the directive to render the HTML -- I've tried myriad angles and approaches -- and have come to the conclusion there are things about directives I'm simply missing.

Mark
  • 9
  • 2
  • Please. **Eliminate any issues that aren't relevant to the problem.** If your question isn’t about a compiler error, ensure that there are no compile-time errors. – georgeawg Feb 27 '17 at 03:39
  • I've just done a [jsFiddle](http://jsfiddle.net/The_Bear/sZZEt/969/) with your problem. Check this [post](http://stackoverflow.com/questions/18157305/compiling-dynamic-html-strings-from-database) to know how to use $compile with html... – The.Bear Feb 27 '17 at 03:54
  • Very Nice!! Really appreciate the quick response and clear reply. Thank you much. I will implement this as soon as I can and give feedback. – Mark Feb 28 '17 at 13:03
  • Your solution worked great (really appreciate it). In addition to this solution I ran into a different issue but I've discovered the '$observe' function (as a solve). I had a value placed in $scope and then used on the page as an attribute in my directive; but the attribute wasn't available until a certain dataset completed loading (the value is the length of the dataset). I kept getting 'undefined' for attributes -- until I discovered the '$observe' function (attributes.$observe('myAttribute',function(){....do stuff here cuz now the attribute value has loaded/changed....}. – Mark Mar 22 '17 at 13:22

0 Answers0