In my app.js I have this method:
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
gvi.Notifications.registerForPush(MessagesService.onNotificationResponse);
});
})
And a factory:
.factory('MessagesService', function($scope, $q) {
var messages = [];
return {
onNotificationResponse: function(sender, message, msgId, msgType, msgUrl) {
console.log("myApp.onNotificationResponse:" + message + " msgUrl:" + msgUrl);
$scope.messages.push({
sender: sender,
message: message,
msgId: msgId,
msgType: msgType,
msgUrl: msgUrl
});
MessagesService.save($scope.messages);
},
}
})
When I open the app, I get this error:
Uncaught ReferenceError: MessagesService is not defined
How do I use the MessagesService factory in the ionicPlatform.ready function?
EDIT:
I have fixed the MessagesService error, now how do I use $scope in the factory?
You have not passed the MessageService
dependency in run
.run(function($ionicPlatform,MessagesService) {
$ionicPlatform.ready(function() {
gvi.Notifications.registerForPush(MessagesService.onNotificationResponse);
});
})
Update: Based on your requirement you need to refactor your service since service cannot refer to scope. You service should expose the message array to scope, rather than using the one defined on scope.
.factory('MessagesService', function($scope, $q) {
var messages = [];
return {
onNotificationResponse: function(sender, message, msgId, msgType, msgUrl) {
console.log("myApp.onNotificationResponse:" + message + " msgUrl:" + msgUrl);
messages.push({
sender: sender,
message: message,
msgId: msgId,
msgType: msgType,
msgUrl: msgUrl
});
MessagesService.save(messages);
},
messages:messages
}
})
Now you can reference this messages collection in any controller using MessageService.messages
property.