编程语言
首页 > 编程语言> > javascript-PubNub AngularJS未定义函数ngSubscribe

javascript-PubNub AngularJS未定义函数ngSubscribe

作者:互联网

我试图遵循pubnub网站上的本指南,将PubNub与AngularJS结合使用

我在app.js中的代码:

var myapp = angular.module('myapp', ["pubnub.angular.service"])
    .controller('MessagesController', ['$scope', '$http', '$log', '$rootScope', function($scope, $http, $log, $rootScope) {
    ....

首先我做$scope.initPubNub();然后调用$scope.subscribe();

$scope.initPubNub = function() {
    if (!$rootScope.initialized) {
        pubNubDemo = PubNub.init({
              publish_key: 'some token here',
              subscribe_key: 'yet another token',
              uuid: $scope.senderId
        });
    };
    $rootScope.initialized = true;
    console.log("PUBNUB inited");
};

最后一种订阅方法:

$scope.subscribe = function(theChannel) {
    console.log("1111aaaa");

    theChannel = typeof theChannel !== 'undefined' ? theChannel : "/chats/3";

    console.log(theChannel);

    console.log("????111");
    cpubNubDemo.ngSubscribe({ channel: theChannel })
    console.log("????222");


  pubNubDemo.subscribe({ channel: theChannel, callback: function() { console.log(arguments); }});

  console.log("zzzzz");

  $rootScope.$on(pubNubDemo.ngMsgEv(theChannel), function(event, payload) {
    // payload contains message, channel, env...
    console.log('got a message event:', payload);
  })

  $rootScope.$on(pubNubDemo.ngPrsEv(theChannel), function(event, payload) {
    // payload contains message, channel, env...
    console.log('got a presence event:', payload);
  })
};

我在浏览器中得到了跟踪:

vvvv
app-1be16923488e02c7beff27c3ad014207.js?body=1:177 PUBNUB inited
app-1be16923488e02c7beff27c3ad014207.js?body=1:266 zzzz
app-1be16923488e02c7beff27c3ad014207.js?body=1:200 1111aaaa
app-1be16923488e02c7beff27c3ad014207.js?body=1:204 /chats/3
app-1be16923488e02c7beff27c3ad014207.js?body=1:206 ????111
app-1be16923488e02c7beff27c3ad014207.js?body=1:207 function e(a){return Jb(a)}
app-1be16923488e02c7beff27c3ad014207.js?body=1:208 ????222
angular-315fad2c2dccaccfc6923f8711bb9301.js?body=1:6350 TypeError: pubNubDemo.ngSubscribe is not a function
    at Object.$scope.subscribe (http://localhost:3000/assets/app-1be16923488e02c7beff27c3ad014207.js?body=1:211:18)
    at useHttp (http://localhost:3000/assets/app-1be16923488e02c7beff27c3ad014207.js?body=1:267:14)
    at elementFns (http://localhost:3000/assets/angular-all-unstable/angular-315fad2c2dccaccfc6923f8711bb9301.js?body=1:7004:19)
    at Object.$get.Scope.$eval (http://localhost:3000/assets/angular-all-unstable/angular-315fad2c2dccaccfc6923f8711bb9301.js?body=1:8927:28)
    at ngDirective.compile.pre (http://localhost:3000/assets/angular-all-unstable/angular-315fad2c2dccaccfc6923f8711bb9301.js?body=1:14919:15)
    at nodeLinkFn (http://localhost:3000/assets/angular-all-unstable/angular-315fad2c2dccaccfc6923f8711bb9301.js?body=1:4946:13)
    at compositeLinkFn (http://localhost:3000/assets/angular-all-unstable/angular-315fad2c2dccaccfc6923f8711bb9301.js?body=1:4551:15)
    at compositeLinkFn (http://localhost:3000/assets/angular-all-unstable/angular-315fad2c2dccaccfc6923f8711bb9301.js?body=1:4554:13)
    at compositeLinkFn (http://localhost:3000/assets/angular-all-unstable/angular-315fad2c2dccaccfc6923f8711bb9301.js?body=1:4554:13)
    at compositeLinkFn (http://localhost:3000/assets/angular-all-unstable/angular-315fad2c2dccaccfc6923f8711bb9301.js?body=1:4554:13) <table class="table table-hover display" id="" ng-init="apiCtrl.useHttp()">(anonymous function) @ angular-315fad2c2dccaccfc6923f8711bb9301.js?body=1:6350$get @ angular-315fad2c2dccaccfc6923f8711bb9301.js?body=1:5421nodeLinkFn @ angular-315fad2c2dccaccfc6923f8711bb9301.js?body=1:4949compositeLinkFn @ angular-
....
12:132 GET localhost:3000 / %7B%7B%20value['profile']['profile_photo']['thumb']%20%7D%7D 500 (Internal Server Error)

它告诉ngSubscribe不是一个函数.我做错了什么?
我用凉亭安装了“ pubnub-angular”组件.
在bower_components / pubnub-angular / lib / pubnub-angular.js中,我看到:

// Generated by CoffeeScript 1.6.3
(function() {
  'use strict';
  angular.module('pubnub.angular.service', []).factory('PubNub', [
    '$rootScope', function($rootScope) {
      var c, k, _i, _len, _ref;
      c = {
        'VERSION': '1.1.0',
        '_instance': null,
        '_channels': [],
        '_presence': {},
        'jsapi': {}
      }; 
      .... 
      c.ngSubscribe = function(args) {
        var _base, _name;
        if (c['_channels'].indexOf(args.channel) < 0) {
          c['_channels'].push(args.channel);
        }
        (_base = c['_presence'])[_name = args.channel] || (_base[_name] = []);
        args = c._ngInstallHandlers(args);
        return c.jsapi.subscribe(args);
      };
      ....

我究竟做错了什么?

解决方法:

它应该是这样的:.controller(‘MessagesController’,[‘$scope’,’$http’,’$log’,’$rootScope’,’PubNub’,function($scope,$http,$log,$rootScope,PubNub){

标签:pubnub,angularjs,javascript,ruby-on-rails
来源: https://codeday.me/bug/20191120/2044291.html