AngularJS: Prepend HTML using Directive

2019-09-09 21:26发布

Service.js

this.showSpinner = function (Id) {

    angular.element("<wave-spinner id='spinner00' ng-show='true'></wave-spinner>").remove();
    var myEl = angular.element(document.querySelector("#header00"))
    myEl.prepend($compile("<wave-spinner id='spinner00' ng-show='true'></wave-spinner>")($rootScope));
};

HTML

<div id="header00" style="">Some Content</div>

I tried using prepend method as mentioned in jQlite, but its not working.

  1. How do I show directive before div header00? .append works with directive but not .prepend to add any html content before matched div element.
  2. When directive is called again, how do I remove existing html. remove() is not working for me.

2条回答
放荡不羁爱自由
2楼-- · 2019-09-09 21:34

Your code only needs a simple change. Switch the before function which does not exist to prepend ( You said that you have tried it but I'm sure this is the solution to this problem ).

myEl.prepend($compile("<wave-spinner id='spinner00' ng-show='true'></wave-spinner>")($rootScope));

To be sure I checked in jsFiddle - https://jsfiddle.net/maciejsikora/u5vLvxmx/

查看更多
干净又极端
3楼-- · 2019-09-09 21:36

You can do it this way:

<wave-spinner id='spinner00' ng-show='isLoadingContent'></wave-spinner> 
<div id="header00" style="">Some Content</div>

and in service.js

 this.showSpinner = function () {
     this.isLoadingContent = true;
 };
查看更多
登录 后发表回答