从AngularJS中的run函数更新$scope对象
我在run函数中有我的通知侦听器。收到通知后,我需要使用通知对象中的参数更新$scope中的对象从AngularJS中的run函数更新$scope对象,angularjs,ionic-framework,Angularjs,Ionic Framework,我在run函数中有我的通知侦听器。收到通知后,我需要使用通知对象中的参数更新$scope中的对象 angular.module('app', ['ionic', 'chatsCtrl']) .run(function($state, $ionicPlatform) { window.FirebasePlugin.onNotificationOpen(function(notification) { // Need to append this notification.paramet
angular.module('app', ['ionic', 'chatsCtrl'])
.run(function($state, $ionicPlatform) {
window.FirebasePlugin.onNotificationOpen(function(notification) {
// Need to append this notification.parameter to a scope variable present in a controller
}
}
.controller('chatsCtrl', function($scope) {
// $scope.chats
});
我该怎么做呢?我不想使用$rootScope对象作为$scope.chat对象,它会变得很重
谢谢您不能在运行块内调用
范围
变量/函数。由于您不想使用rootscope
,我的建议是创建一个服务,并从run块为该服务中的特定方法赋值。然后使用相同的服务从控制器获取该值
angular.module('app', ['ionic', 'chatsCtrl'])
.run(function($state, $ionicPlatform) {
window.FirebasePlugin.onNotificationOpen(function(notification) {
sampleService.setData(notification)
}
}
.controller('chatsCtrl', function($scope,sampleService) {
$scope.chats = sampleService.getData()
});
.factory('sampleService', function() {
var data;
return {
getData : function(){ return data},
setData: function(param){ data = param},
}
});
在这里,
$broadcast
可能是一种更好的方法是的,但是尽量避免像broadcast
这样的听众。它将降低性能并提高性能complexity@sachilaranawaka我执行了你的建议。但这些数据并不持久。所以我必须将它存储在本地存储中,直到我在控制器中检索到它。或者工厂是否持久化数据?@Pradeep Rajashekar如果不再次重写get方法,则应使用$broadcast