【问题标题】:Ionic Pubnub chat in Angular: ionic.bundle.js:21157 TypeError: PubNub.publish is not a functionAngular 中的 Ionic Pubnub 聊天:ionic.bundle.js:21157 TypeError:PubNub.publish 不是函数
【发布时间】:2016-08-25 01:42:10
【问题描述】:

我正在使用 PubNub Api 和 ionic 平台创建 whatsapp 应用程序。但是,ionic.bundle.js:21157 TypeError: PubNub.publish is not a function 会带来问题。下面是我的代码。谁能帮帮我?

var chat = angular.module('chat', ['pubnub.angular.service']);
   chat.controller('chatController', ['$scope', '$state', '$rootScope',        
   '$stateParams', 'PubNub', '$http', '$ionicScrollDelegate', 
    function($scope, $state, $rootScope, $stateParams, PubNub, $http,    
    $ionicScrollDelegate){
          var nickname = $stateParams.nickname;
          var channel = $stateParams.channel;
          $scope.messageContent = '';
          $scope.messages = [];

          $scope.goBack = function() {
          $state.go('channels',{nickname: nickname, channel: channel}); 
      }

     PubNub.init({
        publish_key:'',
        subscribe_key:'',
        ssl: true,
        nickname: nickname
     });

    $scope.sendMessage = function() {
         if (!$scope.messageContent ||
              $scope.messageContent === '') {
                 return;
         }
    PubNub.publish({
        channel: channel,
        message: {
            content: $scope.messageContent,
            nickname: nickname
        },
        callback: function(m) {
            console.log(m);
        }
    });
    $scope.messageContent = '';
}

PubNub.ngSubscribe({channel: channel});

$rootScope.$on(PubNub.ngMsgEv(channel), function(event, payload) {
    $scope.$apply(function() {
        $scope.messages.push(payload.message);
        $ionicScrollDelegate.scrollBottom();
        console.log(payload.message);
    });

  });
}]);

【问题讨论】:

    标签: javascript angularjs ionic-framework chat pubnub


    【解决方案1】:

    要注入的服务名称是 Pubnub 而不是 PubNub。

    【讨论】:

    • 即使使用正确的依赖注入 (Pubnub),我也会遇到同样的错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    相关资源
    最近更新 更多