0

I used some Syntax highlighting API for highlighting code snippet for my web application.To do that i have used highlightjs .I created popup model and inside model i have put <pre> tag and when model open it should display my highlighted xml string.

HTML Code snippet

<pre id="tepXml" ><code class="xml">{{tepXml}}</code></pre>

In AngularJs controller dynamically bind the value to tepXml from server.

AngularJs controller

...$promise.then(function(data){
    $scope.tepXml=data.xml;
}

But the problem was that when i open popup model my xml content is empty.nothing display anything.But when i removed <code class="xml"></code> from <pre> xml content would display with out highlighting.I referred some posts and used $compile in angularJs controller but the problem was still the same.

AngularJs controller with $compile

 var target = angular.element($window.document.querySelector('#tepXml'));
 var myHTML = data.xml;
 target.append( $compile( myHTML )($scope) );

If someone knows where i went wrong please point me out.

Plunker

3
  • Are you calling the highlight api after the tepXml variable has been set? Create a plunker to demo. Commented Mar 25, 2016 at 7:23
  • no i called it top of the page inside <head> Commented Mar 25, 2016 at 7:34
  • Create plunkr, might be simple problem, as far as I can tell. Commented Mar 25, 2016 at 7:36

1 Answer 1

1

The quick answer is to do:

$promise.then(function(data){
  $scope.tepXml=data.xml;
  // Call highlight api
  $timeout(function() {
    $('pre#tepXml code').each(function(i, block) {
    hljs.highlightBlock(block);  //or whatever the correct highlightjs call is.
  });
});

The more Angular way of doing things is to call a jQuery function from Angular is to write a Directive. Something like this:

.directive("highlightCode", function($interval) {
    return {
        restrict: "A",
        scope: { highlightCode: "=" },
        link: function(scope, elem, attrs) {
          $scope.$watch('highlightCode', function() {
            $(elem).find('code').each(function(i, block) {
            hljs.highlightBlock(block);  //or whatever the correct highlightjs call is.
          });
        }
    }
});

Used like this:

<pre id="tepXml" highlight-code="tepXml"><code class="xml">{{tepXml}}</code></pre>
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for reply but still the same.data wont bind.i checked using inspect and <pre> was empty.
i created a plunker and updated my post.i just simply put xml string and tested it.but its works fine.but when i put into my web app it wont highlight.I used inside modal-dialog when button click model would visible and must show the content.but other attribute can see like xml file name etc...but cant see xml content its empty.
and i used some other CSS classes also for my other component could it be a problem?
Thanks for the Plunker - but since it's working it's going to be difficult to replicate. Can you make the plunker show the problem or publish some github code that replicates to help.
thanks for the reply.but my web app is really a huge one so its really hard to create a plunker using peace of code from web app.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.