Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS wait unitl变量已更改_Javascript_Angularjs_Sharepoint - Fatal编程技术网

Javascript AngularJS wait unitl变量已更改

Javascript AngularJS wait unitl变量已更改,javascript,angularjs,sharepoint,Javascript,Angularjs,Sharepoint,(为了更准确地描述当前问题,我更改了标题) 我正在尝试从客户端peoplepicker向任务添加人员,问题是为了添加人员,我需要人员的id,我不知道如何在$scope对象中获取此id 这是获取信息和人员id的代码: function getUserInfo() { // Get the people picker object from the page. var peoplePicker = this.SPClientPeoplePicker.SPClientPeoplePickerDict

(为了更准确地描述当前问题,我更改了标题)

我正在尝试从客户端peoplepicker向任务添加人员,问题是为了添加人员,我需要人员的id,我不知道如何在$scope对象中获取此id

这是获取信息和人员id的代码:

function getUserInfo() {

// Get the people picker object from the page.
var peoplePicker = this.SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerDiv_TopSpan;

// Get information about all users.
var users = peoplePicker.GetAllUserInfo();
var userInfo = '';
for (var i = 0; i < users.length; i++) {
    var user = users[i];
    for (var userProperty in user) { 
        userInfo += userProperty + ':  ' + user[userProperty] + '<br>';
    }
}
$('#resolvedUsers').html(userInfo);

// Get user keys.
var keys = peoplePicker.GetAllUserKeys();
$('#userKeys').html(keys);

// Get the first user's ID by using the login name.
getUserId(users[0].Key);
}

// Get the user ID.
function getUserId(loginName) {
var context = new SP.ClientContext.get_current();
this.user = context.get_web().ensureUser(loginName);
context.load(this.user);
context.executeQueryAsync(
     Function.createDelegate(null, ensureUserSuccess), 
     Function.createDelegate(null, onFail)
);
}

function ensureUserSuccess() {
$('#userId').html(this.user.get_id());
}

function onFail(sender, args) {
alert('Query failed. Error: ' + args.get_message());
}
然后在“ensureUserSuccess()”函数中,我执行以下操作:

userId = this.user.get_id();
最后,在$onSave函数中,我将$scope.user.id设置为userId变量。(onSave功能用于将此项目保存到SharePoint列表)

这在我第二次尝试保存项目时起作用,第一次保存时,userId变量是未定义的,但是如果我再次尝试保存它,它就不是了。在我的onSave函数中,我有:

getUserInfo();
$scope.user.id = userId;

在我的头脑中,我喜欢相信在将$scope.user.id设置为userId变量之前,“getUserInfo”函数已经完成,但现在我坚信这就是问题所在,我如何等待设置user.id直到该函数完成

好的,所以我最初的问题是关于如何将用户ID从peoplepicker设置为$scope对象

我发现函数'initializePeoplePicker'有一个回调函数:

schema['OnUserResolvedClientScript'] = function(){
      getUserInfo();
};

这样做解决了我的问题,每次解析一个用户时,这个函数都会激发,然后在保存之前,我将用户id设置为变量,然后设置为$scope对象

基本上,ensureUserSuccess()是在查询成功时调用的函数。而不是
$('#userId').html(this.user.get_id())
您可以执行以下操作:
$scope.userId=this.user.get_id()我猜。这会将id放在scope对象中,我也这么认为,但该函数超出了$scope的范围,它没有在那里定义。也许我可以重写“getUserId”来返回id?所以我可以这样称呼它:$scope.User.Id=getUserId(loginName),但我不确定如何创建服务,并在ensureUserSuccess()函数中设置该服务中的用户id,例如:
userService.set(this.user.get_id())
。然后在您的服务中创建一个get函数,并执行如下操作:
$scope.user.id=userService.get()好的,我会试试,如果我成功了或成功了,我会稍后再给你回复not@Guinn我用一些新的想法更新了这个问题,想看看吗?我在度假,很高兴你同时找到了答案:)
schema['OnUserResolvedClientScript'] = function(){
      getUserInfo();
};