2017-03-01 52 views
0

我想从过滤器令牌字符串返回给我的控制器从过滤器返回令牌字符串到控制器的角度

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)} 
定义

遇到错误说。这时,也响应是未定义的。最后加入到$ rootScope中,现在我该如何将该令牌值返回给我的控制器?

回答

0
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.