正确检索用户名和有用的值(网站标题、版权等)
Properly retrieve username and useful values (site title, copyright, etc.)
我有一个基于此项目的简单web应用程序(https://github.com/arthurkao/angular-drywall),以NodeJS和AngularJS为前端运行。
我正在尝试建立一个简单的页面,在地图上显示所有连接用户的列表(使用谷歌地图、地理位置和PubNub)。
以下是我实际操作的方式:
angular.module('base').controller('TravelCtrl',
function($rootScope, $scope, NgMap, security, $geolocation, PubNub){
$rootScope.extusers = []; //remote users
$scope.initTravel = function() { //declare the init function
PubNub.init({
subscribe_key: $rootScope.security.keys.psk,
publish_key: $rootScope.security.keys.ppk,
uuid: $rootScope.security.currentUser.username,
ssl: true
});
PubNub.ngSubscribe({
channel: "travel",
state: {
position: {},
}
});
console.log("Loaded Travel");
$geolocation.getCurrentPosition({
timeout: 60000
}).then(function(position) { //when location is retreived
$scope.position = position;
PubNub.ngSubscribe({
channel: "travel",
state: {
position: {
lat: Math.floor($scope.position.coords.latitude*1000)/1000, //decrease accuracy
long: Math.floor($scope.position.coords.longitude*1000)/1000,
},
}
});
$rootScope.$on(PubNub.ngPrsEv("travel"), function(event, payload) {
$scope.$apply(function() {
$scope.extusers = PubNub.ngPresenceData("travel");
});
});
PubNub.ngHereNow({ channel: "travel" });
$scope.showInfo = function(evt, marker) { //show user window on map
$scope.extuser = marker;
$scope.showInfoWindow('infoWindow');
};
});
};
if ($rootScope.hasLoaded()) { //if username and keys are already loaded, then init module
$scope.initTravel();
} else { //else, wait for username and keys to be loaded
$rootScope.$on('info-loaded', function(event, args) {
$scope.initTravel();
});
}
}
);
虽然它能工作,但它看起来很有缺陷,有时只加载。偶尔,我会得到这个:
结果截图
我真的不知道自己做错了什么,因为我只是简单地遵循了PubNub的AngularJS SDK教程。
我认为这与我如何初始化应用程序有关。
angular.module('app').run(['$location', '$rootScope', 'security', function($location, $rootScope, security) {
// Get the current user when the application starts
// (in case they are still logged in from a previous session)
$rootScope.hasLoaded = function() {
return (security.keys && security.info && security.currentUser); //check if everything is loaded correctly
};
$rootScope.checkLoading = function() {
if ($rootScope.hasLoaded()) {
$rootScope.$broadcast('info-loaded'); //broadcast event to "TravelCtrl" in order to init the module
}
};
security.requestKeys().then($rootScope.checkLoading); //request secret keys
security.requestSiteInfo().then($rootScope.checkLoading); //then templating info (site title, copyright, etc.)
security.requestCurrentUser().then($rootScope.checkLoading); //and finally, current user (name, id, etc.)
$rootScope.security = security;
// add a listener to $routeChangeSuccess
$rootScope.$on('$routeChangeSuccess', function (event, current, previous) {
$rootScope.title = current.$$route && current.$$route.title? current.$$route.title: 'Default title';
});
}]);
1-使用JSON API请求密钥、站点信息和当前用户。
2-等待所有内容都加载完毕,然后使用适当的密钥(PubNub、谷歌地图)初始化应用程序
--我的问题是:通过RESTful API检索有用信息后,如何实例化AngularJS应用程序?
我是AngularJS的新手,如果我的方法完全荒谬,我不会感到惊讶,但我真的需要得到一些建议。
提前感谢您的帮助,
Ulysse
您不必等待AJAX查询结束才能启动有角度的应用程序。你可以使用$http承诺(详细信息)
在控制器中:
// Simple GET request example:
$http({
method: 'GET',
url: '/someUrl'
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
// data is now accessible in the html
$scope.data = response ;
// you can call a function to add markers on your maps with the received data
addMarkerOnMap(response);
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
你也可以在一些变量上添加一个手表来等待修改:
// you should have $scope.yourVarName declared.
$scope.$watch('yourVarName', function(newValue, oldValue) {
console.log(newValue);
});
或者观看列表/对象
$scope.$watchCollection('[var1,var2]', function () {
},true);
相关文章:
- 如何在标题上创建弹出框,当点击关闭按钮时,不使用jquery或php在网站中显示
- j查询网站标题问题
- 为网站的所有页面添加标题
- 优化的方法;标题“;只发送一次POST请求即可访问网站
- 如何从网站上的所有单独页面标题中删除不需要的杂物
- 我试图从这个网站获得标题(http://www.itslaw.com),它是通过JavaScript加载的
- 单击我的joomla网站上的模块标题以指向一篇文章
- “index.swf”嵌入在“index.html”中,在一个页面中显示同一网站两次.HTML 标题标签/属性也显示值“
- 在我的网站标题中加载脚本
- 如何使用 JavaScript 修改网站的标题
- 如果我的网站在iframe中,我不希望标题显示在任何页面中
- 网站内容在淡出的标题上滚动
- 当用户离开我的网站时,将标题附加到页面(他们也会这样做)
- 如何将我网站上的所有表格标题定义为JavaScript中的锚点
- 正确检索用户名和有用的值(网站标题、版权等)
- 在中间人中使用全局变量?即网站标题
- 将WordPress博客添加到网站,同时保留我现有的标题和脚本
- 当我的用户收到朋友的未读消息时,我如何切换我的网站标题
- 预加载网站标题横幅&将其添加到缓存
- 如何获得与iso-8859-1编码的页面标题,以便标题将在我的utf-8网站中正确显示