将标记字符串从过滤器返回到控制器角度

时间:2017-03-01 07:46:35

标签: angularjs firebase-cloud-messaging

我想将一个令牌字符串从过滤器返回到我的控制器

    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 个答案:

答案 0 :(得分: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.