I have written one factory to share data between controllers. Here is the sample
.factory('sharedData', function () {
var property = {
title: '',
color: ''
};
return {
getTitle: function () {
return property.title;
},
setTitle: function (title) {
property.title = title;
},
getColor: function () {
return property.color;
},
setColor: function (color) {
property.color = color;
}
};
I am using controller to set data in the factory using.
sharedData.setTitle("title");
but when I am trying to call:
console.log(sharedData.getTitle());
I am receiving empty string. I have injected service in both the controller properly. any help is appreciated.
Edit: Sorry to miss out one important information. I am setting the data after a successful REST call code is as below.
var req =
{
method: 'GET',
url: "/titleName"
headers: {
'Content-type': 'application/json'
}
};
$http(req)
.success(function (response, status)
{
console.log(status);
console.log(response[0].name);
sharedData.setTitle(response[0].name);
sharedData.setColor(response[1].color);
})
.error(function (response, status)
{
});
Json response is as below :
[{"name":"Name"},{"color":"red"}]
console.log in success callback is giving correct response as Name. Thanks
,aftersetTitlein your examplepropertyand take advantage of prototypical inheritance