我收到以下错误 - angular.js:38未捕获错误:[$injector:unpr] http://errors.angularjs.org/1.5.5/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope
我正在尝试使用 pubnub 发送订阅消息,并合并一个 ng 通知栏,用于订阅消息是否成功通过。
控制器的代码如下,我不确定的主要部分是ngNotifications部分(showError,showSuccess,showWarning等)。您将在最后一部分看到我实现了一个 if else 语句来指定何时应用成功通知以及何时应用错误通知:
myApp.controller('NotificationsController',['$scope','$http','$rootScope','$location','token_service','PubNub','notifications', function($scope,$http,$rootScope,$location,token_service,PubNub,notifications){
$scope.noti = [];
function theCallback (x){
$scope.$apply(function () {
$rootScope.spinner = false;
$scope.noti = x[0];
console.log(x[0]);
});
}
$scope.showError = function () {
notifications.showError("Error, are you sure you've followed the correct process?");
};
$scope.showWarning = function () {
notifications.showWarning('Having trouble, please try again.');
};
$scope.showSuccess = function () {
notifications.showSuccess('Welcome to Chui!');
};
$rootScope.defaultInstance.history({channel:"chui_channel", count:100,callback:theCallback});
$rootScope.spinner = true;
$scope.keys='test_notifications'
}])
//myApp.controller('SignoutController',['$scope','$rootScope','$location','token_service','signout_service','$window',function($scope,$rootScope,$location,token_service,signout_service,$window){
myApp.run(function($scope,$rootScope,$location,$templateCache,$window,$cookies,$route,api_service,notifications) {
$rootScope.defaultInstance = PUBNUB.init({
publish_key: 'pub-c-5aace072-1c95-4d6c-8b95-28638c4bd6a2',
subscribe_key: 'sub-c-0ac5d634-10aa-11e6-bbd9-02ee2ddab7fe'
});
var device = api_service.get_devices()
device.then(function(response){
$rootScope.devices = response;
console.log(response);
var deviceSerialList = new String
for (var d in response){
deviceSerialList = String(response[d].DeviceId) +',';
var l = deviceSerialList.split(',').filter(Boolean);
if (l.length == response.length){
listen(deviceSerialList);
console.log(deviceSerialList);
$scope.showSuccess = function () {
notifications.showSuccess('Welcome to Chui!');
};
}
else{
$scope.showError = function () {
notifications.showError("Error, are you sure you've followed the correct process?");
};
}
};
});
function listen(deviceList){
$rootScope.defaultInstance.subscribe({
channel : deviceList,
message : function (message, envelope, channelOrGroup, time, channel) {
alert(message)
console.log(
"Message Received." + "n" +
"Channel or Group : " + JSON.stringify(channelOrGroup) + "n" +
"Channel : " + JSON.stringify(channel) + "n" +
"Message : " + JSON.stringify(message) + "n" +
"Time : " + time + "n" +
"Raw Envelope : " + JSON.stringify(envelope)
)
},
});
};
在 html 页面中,我包含了以下内容:它已经有
myApp.run(function($scope
- 这就是原因。 .run
不需要$scope
注射 - 将其移除。