As my angularjs app grows in complexity, I realize the need to split things into multiple controllers. I am looking to create a factory that can fetch data using http.get and share it across different controllers. While I have managed to pass data into controllers in a simple way, I am unsure about implementing the factory that utilizes http.get. Additionally, I want to ensure that http.get is called only once. For a working example, refer to this plunker
var myApp = angular.module('myApp', []);
myApp.factory('Data2', function($http){
var getDataUrlString = "myhappyurl.com";
return $http.get(getDataUrlString);
})
myApp.factory('Data', function(){
return {message:"cool data from factory"}
})
myApp.controller('firstController', function($scope, Data){
$scope.firstData = Data;
})
myApp.controller('secondController', function($scope, Data){
$scope.secondData = Data;
})
I believe I am on the right track with the 'Data2' factory. The 'Data' factory, which simply returns a string message, works smoothly in the provided example code. I just need assistance in transitioning from a basic string to utilizing http.get.