0

I have an AngularJS app with 2 routes /home and /about.

I'm using ng-router and each route has different controllers HomeCtrl and AboutCtrl. The default route is /home.

The thing is that before display the content I want to add a preloader, just a simple div which will hide when the content is loaded.

 <div class="myApp">
 <div class="preloader"></div>
 <div ui-view></div> 
 </div>

My question is, in which controller should I add this? Should I add a new controller outside the ng-view for this kind of stuff?

Can someone explain me best practice?

asked Feb 26, 2016 at 14:05
1
  • Can be triggered numerous different ways in a directive Commented Feb 26, 2016 at 14:20

3 Answers 3

1

I suppose the best way to do this is to use flag for data loaded indication directle in controller. So depend on this flag with ng-if directive you can show 'div' with loading indicator if dataLoadedFlag is false and div with your data otherwise.

answered Feb 26, 2016 at 14:08
Sign up to request clarification or add additional context in comments.

1 Comment

but the thing is that the preloader its outside ui view jsbin.com/lebakurewa/1/edit?html,output
1

You have ng-view, and your views render over there with its corresponding controller.

So the only thing you need ng-if

<ng-view>
 <div ng-if="!$scope.contentIsReady">
 content loading
 </div>
 <div ng-if="$scope.contentIsReady">
 content here
 </div>
</ng-view>
answered Feb 26, 2016 at 14:09

3 Comments

But the preloader div its outside the ui-view, its something like this jsbin.com/lebakurewa/1/edit?html,output
So put it in side ng-view
hm, are you sure its best practice?
0

@Hiero In your case of course, you have to create new controller.

Also you can use nested views. Something like this:

$stateProvider.state('home', {
 url: '/home',
 views: {
 "main": {
 templateUrl: '....',
 controller: '....'
 },
 'view-with-data@home': {
 templateUrl: '...',
 controller: '...',
 }
 }
 });

See more at https://github.com/angular-ui/ui-router/wiki/Nested-States-&-Nested-Views

answered Feb 26, 2016 at 14:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.