我正在使用AngularJS應用程序。這個應用程序通過過濾和排序等構造了一個複雜的零件信息表。該表使用兩個ngRepeat指令構建。其中一個命中JSON API來獲取列標題,另一個獲取這些列的所有部分數據。根據零件類別,表格可以有5到50列的任意位置,並且該類別中可能有2到2000個零件。AngularJS - 如何僅對多個事件的組合採取行動
表格呈現後,我需要應用jQuery插件來啓用附加功能。我的問題是插件初始化發生得太早。當指令完成所有渲染時,我只需要調用init函數。
我的第一步是創建一個指令,設置一個我可以聽的事件。以下是我使用的:
angular.module('myApp')
.directive('myOnLoad', [function() {
return function(scope, element, attrs) {
if (scope.$last) setTimeout(function(){
scope.$emit('everythingLoaded', element, attrs);
}, 1);
};
}]);
我然後把該指令對這樣我的中繼器之一:
<th ng-repeat="attribute in ctrl.attributes" attribcol="attribute" class="mps-attribute-cell" my-on-load></th>
然後在我的控制器聽吧:
$scope.$on('everythingLoaded', function(scope, element, attrs){
var containerHeight = jQuery(window).height() - 100;
jQuery('#mps-table').fixedHeaderTable({
width: '100%',
height: containerHeight + 'px',
fixedColumn: true
});
});
這幫助我等待插件初始化之前加載的屬性列,但它並不完全正確,因爲當這種情況發生時,其他指令可能無法完成。我需要確保兩個中繼器都已完成。
在init代碼觸發之前,我怎樣才能要求兩個不同事件的組合?
UPDATE:
我創建了一個看起來像這樣至今服務:
angular.module('myApp')
.factory('featureInit', ['$rootScope',
function ($rootScope) {
var featureReady = 0;
$rootScope.$on('attributesLoaded', function (scope, element, attrs) {
featureReady++;
console.log('attributesLoaded so featureReady is now ' + featureReady);
});
$rootScope.$on('partsLoaded', function (scope, element, attrs) {
featureReady++;
console.log('partsLoaded so featureReady is now ' + featureReady);
});
featureInit.init = function() {
if (featureReady > 1) {
console.log('triggers were hit');
var containerHeight = jQuery(window).height() - 100;
jQuery('#mps-table').fixedHeaderTable({
width: '100%',
height: containerHeight + 'px',
fixedColumn: true
});
}
};
}]);
我已經調整的情況下,發射指令產生正確的事件何時完成加載和我服務正確記錄變量「featureReady」。我可以看到它在最終到達「2」時得到記錄,此時我想運行init函數中的所有代碼。
在這一點上,我不確定如何讓我的應用程序「觀看」featureReady變量,以便它在觸及正確的點時啓動該init函數。
您可以創建一個服務,(因爲它們是單身),把你想要觀看裏面的事情的清單它,當你想觀看的所有東西都完成時觸發一個功能。 – Delta
更新了問題。 – claywhipkey
如果您正在使用角UI路由器作爲指定在這裏你可以監聽$ viewContentLoadedevent https://github.com/angular-ui/ui-router/wiki#view-load-events – Senthil