Skip to content

Instantly share code, notes, and snippets.

@alexcheng1982
Created April 16, 2014 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexcheng1982/10847967 to your computer and use it in GitHub Desktop.
Save alexcheng1982/10847967 to your computer and use it in GitHub Desktop.
AngularJS - Delay Controller Initialization for Asynchronous Operations http://midgetontoes.com/blog/2014/04/15/angularjs-delay-controller-initialization-for-asynchronous-operations/
angular.module('controllers', ['services'])
.controller('MyCtrl', ['$scope', ($scope) ->
$scope.$watch('dataReady', (ready) ->
init() if ready
)
init = () ->
console.log('Init')
])
angular.module('services', [])
.run(['$rootScope', 'dataService', ($rootScope, dataService) ->
dataLoaded = false
$rootScope.dataReady = false
dataService.loadData().then(() ->
dataLoaded = true
)
$rootScope.$watch(() ->
dataLoaded
, (ready) ->
$rootScope.dataReady = ready
)
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment