1

I have HTML code:

<div dynamic="html"></div>

And Angular JS in AJAX response:

$scope.html = content;

My dirrective dynamic looks as:

.directive('dynamic', function ($compile) {
            return {
                restrict: 'A',
                replace: true,
                link: function (scope, ele, attrs) {
                    scope.$watch(attrs.dynamic, function(html) {
                        ele.html(html);
                        $compile(ele.contents())(scope);
                    });
                }
            };
        })

So, when I have $scope.html with HTML code in response then dynamic does not insert(prepend) this code in div block. Why?

1

3 Answers 3

1

...And Angular JS in AJAX response:

I cannot see any ajax call being made to get the template.

So, when I have $scope.html with HTML code in response then dynamic does not insert(prepend) this code in div block. Why?

You are not seeing any content because html, the callback response in $watch is undefined

From my understanding, I presume you want to have a directive attribute with value supplied as an html file where you could make an ajax request to.

<div dynamic="file.html"></div>

Further, in the link:{ ... } function of your attribute, you can make an ajax call to supplied template path, get its html as response data and insert/append that into the div block containing your directive.

 link: function (scope, ele, attrs) {
   $http.get('file.html').then(function(content){
     ele.html(content.data);
     $compile(ele.contents())(scope);
   })
 }        

Here's the demo

Sign up to request clarification or add additional context in comments.

1 Comment

Seems it that i need but instead file I have returned HTML block, that need insert to page
0

I would recommend you to use ngTransclude to do this.

That allows you to do something like:

<div yourdirective>
   <h1> Some custom HTML</h1>
</div>

Then if you're directive must include <ngTransclude></ngTransclude> which will then include the HTML that you put inside, into the directive.

Documentation here: https://docs.angularjs.org/api/ng/directive/ngTransclude

2 Comments

How I can insert HTML from Ajax to this directive?
Sure - you can just use an {{expression}} in there AFAIK
0

Your code Working fine with proper binding of angular variable. See here the Plunkr which give you the Idea what you are missing in your code.

Update

As you want to replace directive element with your content you need to use ele.replaceWith that will replace the directive element.

Code

link: function(scope, ele, attrs) {
  var watcher = scope.$watch(attrs.dynamic, function(newhtml, oldhtml) {
    if(newhtml){
      ele.replaceWith($compile(newhtml)(scope));
      watcher(); //destructing watcher
    }
  });
}

Working Plunkr Here

10 Comments

Tried you solution, does not work foe me, not prepend HTML in block div. I get error also: $sce.trustedAsHtml
@Caur it appends see here but binding won't work plnkr.co/edit/rXBaMLoNDEBknDip6k1H?p=preview
No, i need re-compile HTM because there are directory in HTML
@Caur do you want it to prepent to your directive div?
Yes, I want that HTM will be insert to div and was compile
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.