I've been encountering an issue that I haven't been able to resolve despite searching for solutions.
Currently, I am attempting to share data between two Angular controllers in CoffeeScript:
angular.module('app').controller 'accountCtrl', ($scope, $state, accountFactory) ->
Api.Account.get(token: $scope.current_user.account_id).$promise.then ((response) ->
accountFactory.set(response)
$scope.account = accountFactory.account
)
angular.module('app').controller 'accountPaymentMethodsCtrl', ($scope, $state, Api, accountFactory) ->
$scope.account = accountFactory.account
$scope.displayedPaymentMethods = [].concat($scope.account.payment_methods)
The data is stored in a factory like this:
angular.module('app').factory 'accountFactory', ->
account = {
payment_methods: {}
set: (account) ->
@account = account
@account.payment_methods = account.payment_methods
}
account
However, upon refreshing the page,
$scope.displayedPaymentMethods = [].concat($scope.account.payment_methods)
triggers an error message stating
"cannot read property 'payment_methods' of undefined"
This occurs because $scope.account in the parent controller has not been resolved yet. How can I ensure that this line and others execute only after $scope.account in the parent controller has been resolved?