2015-11-06 52 views
1

我們有一個在工作中由一些人寫的角柵格,整個公司都使用它。 一款TD-細胞可能看起來像這樣反向角元素

<td typeahead-cell="location as location.Name for location in getApiLocations($viewValue, mapping)" ng-model="mapping.selectedLocation"> 
    {{mapping.getLocationNames()}} 
</td> 

typeahead-cell指令將執行對TD一些自定義代碼,它的作用是轉播一些代碼,因此,如果您雙擊或在單元格中寫它將從去只顯示(在這種情況下)鍵入。它通過獲取td單元格中的html(td單元已經按照角度編譯)來完成此工作,用一些自定義代碼包裝它,然後調用$ compile編譯整個事物。這適用於上面的表達式,如{{mapping.getLocationNames()}},因爲它們在編譯時不會更改,因此可以編譯任意次數。

我現在面臨的問題是我嘗試使用更復雜的表達式ng-repeat。問題是所述第一編譯(由角芯直接完成)

從將來自實施例的HTML變

<span ng-repeat="location in mapping.locations">...</span> 

<!-- ngRepeat: location in mapping.locations --> 

然後,當我們的自定義網格代碼executs它將試圖編譯上面的代碼會導致一個空的,因爲它編譯的HTML評論。

這是打破

$element.html($compile(displayElement.html($element.html()))($scope)); 

$元素是TD-細胞包含我的一部開拓創新的代碼,做$element.html()什麼時候會編譯後的代碼,並嘗試使用該代碼。不會工作。 Displayelement是一個包裝,它將顯示我們在displaymode中的時間。

我要麼需要反編譯$element以便編輯$element.html或以某種方式移動已編譯和掛接的$ element(td單元格)的內容。

任何想法?

編輯:我有所解決它,這樣做

$element.children().appendTo(displayElement); 
displayElement.appendTo($element); 

這將需要從TD小區內的孩子,並將其添加到顯示元素,而無需實際打破了原有的$compile。所以,如果你有一個表達jQuery.children不能移動<!-- comment -->元素與納克像我repater指令上面,你需要將它包裝在一個虛設的元素像

<span><span ng-repeat="location in mapping.locations">...</span></span> 

任何解決方法嗎?

回答

0

最終的解決方案是這樣的

$element.contents().appendTo(displayElement); 
displayElement.appendTo($element); 

它使用contents是非常重要的,而不是因爲childrenchildren忽略,不會包括由ng-repeat指令產生的意見文本節點。

0

該行,如果你可以用這個

//Store it first on a variable if blank 
var html; 
if(!html) html = displayElement.html($element.html()); 
$element.html($compile(html)($scope)); 

檢查希望它會工作的Instated。可能是你需要管理變量的範圍。

+0

這不會工作,$ element.html(),因爲角已經爲你編譯它,請參閱我的編輯 – Anders