如何从 $.get jquery 更新 angularjs $scope?
Posted
技术标签:
【中文标题】如何从 $.get jquery 更新 angularjs $scope?【英文标题】:How to update angularjs $scope from $.get jquery? 【发布时间】:2017-10-31 20:43:56 【问题描述】:我正在尝试使用以下代码并使用$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
中获得响应,但无法在其他地方获得数据。
如何在$scope
中使用该值?
尝试关注this question,并申请$scope.$apply()
,但没有成功。
【问题讨论】:
当你做console.log(invitePeersController.gmailContacts);
时,它会记录数据吗?
@anoop 是的,确实如此。
控制台有错误吗?您是在注入$scope
控制器吗?还是这是service
?作为建议,您应该使用$http.get()
【参考方案1】:
您需要移动以下块:
InvitePeersService.setGmailContactsData(invitePeersController.gmailContacts);
console.log(invitePeersController.gmailContacts);
$scope.$apply(function ()
$scope.gmailData = invitePeersController.gmailContacts;
console.log($scope.gmailData);
)
在
function(response)
block,以便初始化 invitePeersController.gmailContacts
- 因为响应来自 callback 函数。
因此:
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?的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Visual Studio 2015 更新 3 中的构建中排除 node_module 文件夹