angular.element replaceWith 不起作用

angular.element replaceWith 不起作用

问题描述:

我有一个要替换的元素,并且 html 文本是可变的 h第一次调用这个功能完美.但是第二次调用没有.

I have an element to replace and the html text is in varaiable h The first call to this works perfectly. But the second call does not.

var newElement;
var h="<div>a</div>";
newElement = angular.element(h);
$compile(newElement[0])($scope);        
angular.element($element[0]).replaceWith(newElement[0]);

我认为这里的错误是你已经编译了 html 模板,但你还没有使用它.

I think the mistake here is you have compiled the html template but u havent used it .

var newElement;
var h="<div>a</div>";
newElement = angular.element(h);

angular.element($element[0]).replaceWith($compile(newElement[0])($scope));

希望这个解决方案有效