gpt4 book ai didi

javascript - angularjs 获取所选 ng-repeat 选项的另一个属性以传递到另一个模型中

转载 作者:太空宇宙 更新时间:2023-11-04 16:00:33 24 4
gpt4 key购买 nike

我有一个用户列表,我在列表上执行了 ng-repeat,如下所示。
我将用户的 id 传递给模型,但是,我尝试将同一选定用户的电子邮件传递给另一个模型,例如:

<span>The selected user's email is : {{myModel.email}} </span>

但我什么也没得到。失败!我应该做什么来修复它?

<div class="form-group">
<ui-select ng-model="myModel.user" name="user" theme="selectize" required ng-change="myModel.email = user.email" title="Choose user">
<ui-select-match placeholder="Select user">{{$select.selected.firstName}} {{$select.selected.lastName}}
</ui-select-match>
<ui-select-choices repeat="user.id as user in userList | userFilter:$select.search">
<div ng-bind-html="user.firstName + ' ' + user.lastName | highlight: $select.search"></div>
<small>
email: {{user.email}}
</small>
</ui-select-choices>
</ui-select>
<br/>
<span>The selected user's email is : {{myModel.email}} </span>
</div>

这是 Controller :

$firebaseArray(MyDataBase.ref('users').orderByKey()).$loaded().then(function (res) {
for (var i = 0; i < res.length; i++) {
if (res[i].$id !== current.uid) {
$scope.userList.push({
id: res[i].$id,
firstName: res[i].firstName,
lastName: res[i].lastName,
email: res[i].email
});
} else {
defaultUser = res[i];
}
}
});

最佳答案

先创建一个函数,循环选择的id来获取对应的email:

$scope.userModelEmail = {};
$scope.getUserEmail = function (id) {

for (var i = 0; i < $scope.userList.length; i++) {
if ($scope.userList[i].id === id){

return $scope.userModelEmail.email = $scope.userList[i].email;
}
}

console.log($scope.userModelEmail.email);

}

当然,使用 ng-change 将 id 传递给函数:

ng-change="getUserEmail(myModel.user)"

您可以在这里反射(reflect)它:

<span>The selected user's email is : {{userModelEmail.email}} </span>

关于javascript - angularjs 获取所选 ng-repeat 选项的另一个属性以传递到另一个模型中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42319716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com