以下代碼需要在2個不同的控制器中(目前,稍後可能會有更多的控制器)。代碼解決了我在ng-grid中發現的一個問題,並允許延遲選擇一行(一旦數據被加載)。你在哪裏把這種控制器代碼放在一個角度應用程序中?
// Watch for the ngGridEventData signal and select indexToSelect from the grid in question.
// eventCount parameter is a hack to hide a bug where we get ngGridEventData spam that will cause the grid to deselect the row we just selected
function selectOnGridReady(gridOptions, indexToSelect, eventCount) {
// Capture the grid id for the grid we want, and only react to that grid being updated.
var ngGridId = gridOptions.ngGrid.gridId;
var unWatchEvent = $scope.$on('ngGridEventData', function(evt, gridId) {
if(ngGridId === gridId) {
//gridEvents.push({evt: evt, gridId:gridId});
var grid = gridOptions.ngGrid;
gridOptions.selectItem(indexToSelect, true);
grid.$viewport.scrollTop(grid.rowMap[0] * grid.config.rowHeight);
if($scope[gridOptions.data] && $scope[gridOptions.data].length) {
eventCount -= 1;
if(eventCount <= 0) {
unWatchEvent(); // Our selection has been made, we no longer need to watch this grid
}
}
}
});
}
我現在的問題是我在哪裏放這個公共代碼?這顯然是UI代碼,所以它似乎不屬於服務,但沒有經典的繼承方案(我已經能夠發現),這將允許我把它放在一個「基類」
理想情況下,這將是ng-grid的一部分,不會涉及這樣一個討厭的黑客攻擊,但是ng-grid 2.0對特徵是封閉的,ng-grid 3.0是誰知道未來有多遠。
另一個摺痕是$ scope,我想我必須注入這個代碼,如果我從當前控制器中取出它。
這是否真的屬於服務?
你可以嘗試使用一個mixin。 http://digital-drive.com/?p=188 – ivarni