1

我有一個用戶列表,我在列表中做了一個ng-repeat,如下所示。
我傳遞用戶的ID模型,但是,我想同樣的選擇的用戶的電子郵件傳遞到另一種模式,如:angularjs將選定的ng-repeat選項的另一個屬性傳遞給另一個模型

<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> 

這裏是控制器:

$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]; 
     } 
    } 
}); 

回答

1

通過選擇的ID第一和循環創建一個函數來獲取相應的電子郵件:

$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)" 

你可以在這裏反映:

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