Вернуть строку токена из фильтра в контроллер угловой
Я хочу вернуть строку токена из фильтра на мой контроллер
app. factory('PushNotification', function($rootScope,$q) {
var Service = {};
Service.getTokenKey = getTokenKey;
const messaging = firebase.messaging();
function getTokenKey(){
messaging.requestPermission()
.then(function() {
console.log('Notification permission granted.');
return messaging.getToken();
}).then(function (token) {
console.log('NOTIFICATION TOKEN ', token);
$rootScope.token = token;
})
.catch(function(err) {
console.log('Unable to get permission to notify.', err);
});
return Service;})
Код контроллера:
Notificationservice.getTokenKey().then(function(response){
$scope.token = response )}
Получил ошибку, сказав, что.then определен, а ответ не определен. наконец, добавлено в $rootScope, теперь, как мне вернуть это значение токена в мой контроллер?
1 ответ
Try this:
app. factory('PushNotification', function($rootScope,$q) {
var Service = {};
Service.getTokenKey = getTokenKey;
const messaging = firebase.messaging();
function getTokenKey(){
messaging.requestPermission()
.then(function() {
console.log('Notification permission granted.');
messaging.getToken().then(function (token) {
console.log('NOTIFICATION TOKEN ', token);
// $rootScope.token = token;
return token; // try this
})
.catch(function(err) {
console.log('Unable to get permission to notify.', err);
});
return Service;})
if this is not working, please provide clear description or plunker, so that we can work it out.