Javascript 如何从$.get jquery更新angularjs$范围?
我正在尝试使用以下代码并使用Javascript 如何从$.get jquery更新angularjs$范围?,javascript,jquery,angularjs,angularjs-scope,google-contacts-api,Javascript,Jquery,Angularjs,Angularjs Scope,Google Contacts Api,我正在尝试使用以下代码并使用$scope: var scopes = "https://www.googleapis.com/auth/contacts.readonly"; setTimeout(authorize(), 20); function authorize() { gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthorization); } invi
$scope
:
var scopes = "https://www.googleapis.com/auth/contacts.readonly";
setTimeout(authorize(), 20);
function authorize() {
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthorization);
}
invitePeersController.gmailContacts = [];
function handleAuthorization(authorizationResult) {
if (authorizationResult && !authorizationResult.error) {
$.get("https://www.google.com/m8/feeds/contacts/default/thin?alt=json&access_token=" + authorizationResult.access_token + "&max-results=50000&v=3.0",
function(response){
//process the response here
console.log(response);
var jsonChildData = JSON.parse(JSON.stringify( response.feed.entry));
for(var i=0; i<jsonChildData.length;i++){
try {
var item = {};
var name = JSON.stringify(jsonChildData[i].title.$t);
var email = JSON.stringify(jsonChildData[i].gd$email[0].address);
if(name.substring(1, name.length-1) && email.substring(1, email.length-1)){
item ["name"] = name.substring(1, name.length-1);
item ["email"] = email.substring(1, email.length-1);
item ["id"] = email.substring(1, email.length-1).replace(/[^a-zA-Z ]/g, "");
invitePeersController.gmailContacts.push(item);
}
}
catch(err) {
// console.log("Something went terribly wrong while trying to fetch Gmail Contacts Data");
}
}
InvitePeersService.setGmailContactsData(invitePeersController.gmailContacts);
console.log(invitePeersController.gmailContacts);
$scope.$apply(function(){
$scope.gmailData = invitePeersController.gmailContacts;
console.log($scope.gmailData);
})
});
}
}
}
var作用域=”https://www.googleapis.com/auth/contacts.readonly";
setTimeout(authorize(),20);
函数authorize(){
gapi.auth.authorize({client_id:clientId,scope:scopes,immediate:false},handleAuthorization);
}
InviterPeersController.gmailContacts=[];
功能手动授权(授权结果){
if(authorizationResult&!authorizationResult.error){
$.get(”https://www.google.com/m8/feeds/contacts/default/thin?alt=json&access_token=“+authorizationResult.access_token+”&max results=50000&v=3.0”,
功能(响应){
//在这里处理响应
控制台日志(响应);
var jsonchildata=JSON.parse(JSON.stringify(response.feed.entry));
对于(var i=0;i您需要移动以下块:
InvitePeersService.setGmailContactsData(invitePeersController.gmailContacts);
console.log(invitePeersController.gmailContacts);
$scope.$apply(function () {
$scope.gmailData = invitePeersController.gmailContacts;
console.log($scope.gmailData);
})
在
function(response){
块,以便初始化invitePeersController.gmailContacts
,因为响应来自回调函数
因此:
var scopes = "https://www.googleapis.com/auth/contacts.readonly";
setTimeout(authorize(), 20);
function authorize() {
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthorization);
}
invitePeersController.gmailContacts = [];
function handleAuthorization(authorizationResult) {
if (authorizationResult && !authorizationResult.error) {
$.get('https://www.google.com/m8/feeds/contacts/default/thin?alt=json&access_token=' + authorizationResult.access_token + '&max-results=50000&v=3.0',
function (response) {
//process the response here
console.log(response);
var jsonChildData = JSON.parse(JSON.stringify(response.feed.entry));
for (var i = 0; i < jsonChildData.length; i++) {
try {
var item = {};
var name = JSON.stringify(jsonChildData[i].title.$t);
var email = JSON.stringify(jsonChildData[i].gd$email[0].address);
if (name.substring(1, name.length - 1) && email.substring(1, email.length - 1)) {
item ['name'] = name.substring(1, name.length - 1);
item ['email'] = email.substring(1, email.length - 1);
item ['id'] = email.substring(1, email.length - 1).replace(/[^a-zA-Z ]/g, '');
invitePeersController.gmailContacts.push(item);
}
InvitePeersService.setGmailContactsData(invitePeersController.gmailContacts);
console.log(invitePeersController.gmailContacts);
$scope.$apply(function () {
$scope.gmailData = invitePeersController.gmailContacts;
console.log($scope.gmailData);
})
}
catch (err) {
// console.log("Something went terribly wrong while trying to fetch Gmail Contacts Data");
}
}
});
}
}
var作用域=”https://www.googleapis.com/auth/contacts.readonly";
setTimeout(authorize(),20);
函数authorize(){
gapi.auth.authorize({client_id:clientId,scope:scopes,immediate:false},handleAuthorization);
}
InviterPeersController.gmailContacts=[];
功能手动授权(授权结果){
if(authorizationResult&!authorizationResult.error){
$.get('https://www.google.com/m8/feeds/contacts/default/thin?alt=json&access_token=“+authorizationResult.access_token+”&max results=50000&v=3.0',
功能(响应){
//在这里处理响应
控制台日志(响应);
var jsonchildata=JSON.parse(JSON.stringify(response.feed.entry));
for(var i=0;i
还是一样。当您执行控制台.log(invitePeersController.gmailContacts);
时,它会记录数据吗?@anoop是的,它会。控制台中有错误吗?您是注入$scope
控制器?还是这是一项服务?
?建议您使用$http.get()