如何从$ .get jquery更新angularjs $ scope? [英] How to update angularjs $scope from $.get jquery?
本文介绍了如何从$ .get jquery更新angularjs $ scope?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下代码并使用 $ scope
:
I am trying to use the following code and use $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);
})
});
}
}
}
我可以在 $ scope
中获取响应,但无法在其他地方获取数据。
I can get the response in $scope
but cannot get the data elsewhere.
如何在 $ scope
中使用该值?
尝试按照此问题进行操作,已应用 $ scope。$ apply()
但它无效。
Tried to follow this question, and applied $scope.$apply()
but it is not working.
推荐答案
您需要移动以下块:
InvitePeersService.setGmailContactsData(invitePeersController.gmailContacts);
console.log(invitePeersController.gmailContacts);
$scope.$apply(function () {
$scope.gmailData = invitePeersController.gmailContacts;
console.log($scope.gmailData);
})
function(response){
阻止,以便 invitePeersController.gmailContacts
已初始化 - 因为响应来自回调函数。
block, so that the invitePeersController.gmailContacts
is initialized - as the response comes in a callback function.
因此:
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");
}
}
});
}
}
这篇关于如何从$ .get jquery更新angularjs $ scope?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文