我想返回一个令牌字符串从过滤器返回到我的控制器
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 )}
出错说。最后添加到$ rototscope,现在如何将该令牌值返回到控制器??
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.