我們正在嘗試動態加載和編譯DOM元素,以便進行AB測試。到目前爲止,該代碼工作得很好:動態加載模板會在頁面上留下舊的模板/範圍
MyApp.directive('testing',
['$http', '$templateCache', '$compile', '$window',
function($http, $templateCache, $compile, $window) {
return {
restrict: 'A',
compile: function(tElement, tAttrs, transclude) {
return {
pre: function(scope , elm, iAttrs) {
var test = iAttrs.testing
//Ensure the test is in place
if (typeof angular.testing[test]== "undefined" || !angular.testing[test].template)
return false;
//Build the template path
var tmpName = "/angular/app/testing/"+test+"/"+angular.testing[test].template+".html"
//Hide the original template so the change doesn't flash
angular.element(elm).css('display', 'none')
//Retrieve the new template
$http.get(tmpName, {cache: $templateCache}).success(function(tplContent){
//Compile it
var compiled = $compile(tplContent)(scope)
//Replace the element with the new compiled template
elm.replaceWith(compiled)
})
}
}
}
}
}])
然後,可以將它放在這樣的事情:
<div testing="mytest"></div>
而且您的測試軟件可以與一些設置變化這樣
angular.testing.mytest = {template: "newtest"}
我看到的問題是,被替換的元素仍然存在於頁面上(雖然隱藏在某處 - 我實際上沒有看到它,但我可以通過一些調試找到它),並且它導致一些實習生al指令失敗。另外,試圖刪除它或將其歸零不會產生任何影響。
有什麼建議嗎?
請在plunker中重現此操作。我不確定這是什麼意思,元素「隱藏在某處......但我可以通過一些調試找到它」。另外,什麼是「原始模板」? –
@NewDev這是一個plunkr:http://plnkr.co/edit/YtQknR3FXaxKwhfXXKCO?p=preview。要查看我在說什麼,請打開控制檯並單擊「新建」 - 應該只有一個元素觸發脫機單擊插件,但原始元素也會觸發。這是我的問題的根源。 –