2017-03-01 54 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. 
相關問題