PubNub AngularJS 未定义函数 ngSubscribe
PubNub AngularJS undefined function 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 依赖项.
,我只能让它工作
我试图按照 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 依赖项.
,我只能让它工作