Created
July 20, 2015 22:19
-
-
Save jhines2k7/ec4f8f349ce1c5a14210 to your computer and use it in GitHub Desktop.
Angular async data, controllers and dependencies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
angular.module('main', ['dep1', 'dep2']) | |
.controller('Controller1', function($scope, ServiceOne, ServiceThree){ | |
$scope.fromService = ServiceOne.publicStuff; | |
ServiceThree.getServiceThreeData().then(function(data) { | |
$scope.fromServiceThree = data; | |
}); | |
}) | |
.controller('Controller2', function($scope, $rootScope, ServiceOne, ServiceTwo){ | |
$scope.fromService = ServiceOne.publicStuff; | |
ServiceTwo.getTimeStamp().then(function(data){ | |
$scope.fromServiceTwo = data; | |
}); | |
$scope.getTimeStamp = function() { | |
ServiceTwo.getTimeStamp().then(function(data){ | |
$rootScope.fromClick = data; | |
}); | |
} | |
}) | |
.controller('Controller3', function($scope, ServiceOne, ServiceTwo){ | |
$scope.fromService = ServiceOne.publicStuff; | |
ServiceTwo.getTimeStamp().then(function(data){ | |
$scope.fromServiceTwo = data; | |
}); | |
}); | |
angular.module('dep1', []) | |
.factory('ServiceOne', function(){ | |
var privateStuff = 'This is private'; | |
var publicStuff = 'This is public data from Service One!'; | |
return { | |
publicStuff: publicStuff | |
} | |
}); | |
angular.module('dep2', []) | |
.factory('ServiceTwo', function($q, $timeout){ | |
var timeStamp; | |
return { | |
getTimeStamp: function(){ | |
var defer = $q.defer(); | |
if(typeof timeStamp === 'undefined'){ | |
$timeout(function(){ | |
timeStamp = Date.now(); | |
defer.resolve(timeStamp); | |
}, 2000); | |
}else{ | |
defer.resolve(timeStamp); | |
} | |
return defer.promise; | |
} | |
} | |
}); | |
angular.module('dep2') | |
.factory('ServiceThree', function($q, $timeout) { | |
return { | |
getServiceThreeData: function() { | |
var defer = $q.defer(); | |
$timeout(function() { | |
defer.resolve('Resolved after 4 seconds in Service Three!'); | |
}, 6000); | |
return defer.promise; | |
} | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment