I have a page that has about 8 tabs, and each tab has lot of HTML controllers (data bindings). So data rendering becomes slow.
So I used ng-if condition based on active tab which is found using ng-click on tab. The active tab is made visible using a CSS class. After using the ng-if condition, the default tab gets loaded fast, and on subsequent clicks on other tabs, each tab gets loaded. But each tab takes about 3 to 6 seconds to render, and at this point the user does not know what is happening and whether the page is loading. So I am planning to include a loading spinner.
Now I want to show a angularjs loading spinner when I click on each tab until the tab gets rendered completely. (The tab will not become active until ng-repeat gets completed as it is keeps rendering). So I planned to load the spinner during the ng-click event of the tab. The loading spinner is removed when ng-repeat gets completed. But the problem is with the spinner gets staring part. Even if I start the spinner loading logic in ng-click, it start almost at the end of tab rendering and spinner stops immediately as ng-repeat gets completed. The problem is due the page rendering background process which makes even the spinner to gets stuck and load it properly. So, how can I use a loader or loading message for tab rendering? Below is my sample code for page, ng-click event and to find the end event of ng-repeat.
html code:
<div class="claimant-data-nav " ng-controller="MyController">
<ul class="nav nav-tabs ">
<li ng-repeat="tabname in Mylist | unique:'TabName'" ng-class="{'active':tabname.TabName == 'Tab1'}">
<a data-target="#tab-{{tabname.TabName}}" data-toggle="tab" ng-click="activetab(tabname.TabName)">{{tabname.TabName}}</a>
</li>
</ul>
<div class="tab-content">
<a data-toggle="tab" class="mobile-tabs" data-target="#tab-{{tabname1.TabName}}" href="javascript:void(0)" ng-repeat="tabname1 in Mylist | unique:'TabName'">{{tabname1.TabName}}</a>
<div id="tab-{{tabname2.TabName}}" ng-class="{'tab-pane fade active in':tabname2.TabName == 'Tab1','tab-pane fade':tabname2.TabName != 'Tab1'}" ng-repeat="tabname2 in Mylist | unique:'TabName'">
<div ng-if="activetab == tabname2.TabName">
<div ng-repeat="item in items" on-finish-render="ngRepeatFinished">
<!-- data binding logic here.It has lot of angularjs databindings.-->
</div>
</div>
</div>
</div>
</div>
in the ng-click i activated the loader
.scope.activetab = function(tabname){
showloader();
//some other codes here to set active tab etc.
}
To check ng-repeat is complete i have used below directive
app.directive('onFinishRender', function ($timeout) {
return {
restrict: 'A',
link: function (scope, element, attr) {
if (scope.$last === true) {
$timeout(function () {
scope.$emit('ngRepeatFinished');
});
}
}
}
});
$scope.$on('ngRepeatFinished', function(ngRepeatFinishedEvent) {
removeLoader();
});