angularjs - AngularJS,在外部事件上重新初始化(更新页面)
问题描述
在打开正在加载的聊天页面消息后,我有 angularjs 移动应用程序:
myApp.controller('ChatControllerGlobal',function($scope,global) {
$http({
method: 'POST',
url: serverURL+'&act=get_chat',
withCredentials: true,
}).then(function successCallback(data) {
$scope.messages = data.data;
$scope.loader = false;
$scope.content = true;
});
});
现在通过外部事件我需要以某种方式重新初始化控制器(加载新消息):
window.FirebasePlugin.onNotificationOpen(function(data) {
// some code here to reload new chat messages
});
是否有任何不脏的方法来重新初始化控制器或调用控制器功能?
解决方案
您可以从 Firebase 插件创建可注入服务。然后将该服务注入您的控制器,并调用一个获取消息的函数:
myApp
.factory('firebase', function ($window) {
return $window.FirebasePlugin;
});
myApp.controller('ChatControllerGlobal', function ($scope, $http, firebase) {
getMessages();
firebase.onNotificationOpen(function (data) {
getMessages();
});
function getMessages() {
$http({
method: 'POST',
url: serverURL + '&act=get_chat',
withCredentials: true,
}).then(function successCallback(data) {
$scope.messages = data.data;
$scope.loader = false;
$scope.content = true;
});
}
});
这样您就可以firebase
在单元测试中模拟服务。
推荐阅读
- javascript - JavaScript:时间和日期
- shacl - 如何使用 SHACL 检查多个类实例
- google-cloud-platform - 服务帐户如何在 gcloud 中进行身份验证而不更改环境中的默认 gcloud 帐户?
- python - Kivy image not loading from source
- c++ - 如何在不使用命名空间 chrono 的情况下初始化 std::chrono::duration 常量?
- c# - The connection was not closed when I use using
- ios - IRLDocument Scanner Page Snap cameraview 关闭不会返回到当前的 VC Swift XCode
- python - 在 psycopg2 中为 PostgreSQL 查询使用多个游标有什么好处?
- nlp - PyTorch conv1D 如何处理可变长度序列的填充?
- c# - 使用 IIS 时出现多个错误