I succesfuly created a factory that gets a .php file output (JSON),
My question is how do I access it from wihtin the controller:
myApp = angular.module("myApp", [])
myApp.factory "mainData", ($http) ->
$http.get('gethome.php').success (data) ->
data: data
console.log(data)
myApp.controller "HomeCtrl", ($scope, mainData) ->
$scope.home = mainData.data
Please let me know if Im choosing the right syntax here, I see lots of examples on how to create a module/controller everywhere in tutorials, and Im looking for the right way
When injecting factories into Angular controllers, the injected factory reference contains those members that you've returned from the factory:
But, the problem with your code is that mainData.data will always be undefined, because factory will return before the async $http request is completed. So instead, your factory should be returning a promise:
Note: $http methods always return a promise by default.
You can use this: