【问题标题】:Filling inputs based on selected option in AngularJS根据 AngularJS 中的选定选项填充输入
【发布时间】:2017-02-09 01:30:07
【问题描述】:

我有 3 个输入字段,第一个字段是 select 类型,其余两个是 text 类型。现在,如果我从下拉列表中选择一个值,则相应的值应出现在剩余的两个输入字段中。如何在 AngularJS 中做到这一点。谢谢!

例如如果我从下拉列表中选择 User1,则 User1 对应的电子邮件和电话号码应出现在相应的输入字段中。

注意:值是根据所选选项从数据库中获取的

<select>
    <option value="user1">User1</option>
    <option value="user2">User2</option>
    <option value="user3">User3</option>
    <option value="user4">User4</option>
</select>
<input type="text" id="eamil" placeholder="Email">
<input type="text" id="phone_no" placeholder="Phone number">

【问题讨论】:

  • 向我们展示你的代码人?
  • 我不知道如何实现这一点,所以请告诉我方法
  • 你能告诉我们你的完整代码吗?
  • @Loading.. 这只是一个任务,不是项目的一部分。

标签: angularjs input autocomplete autofill


【解决方案1】:
<select ng-options="s as s in Users" ng-model="user" ng-change = "GetData(user)">

<input type="text" id="eamil" placeholder="Email" ng-model="userEmail">

<input type="text" id="phone_no" placeholder="Phone number" ng-model="userPhone">

现在在控制器上

$scope.GetData = function(user){
  // Here Please add Code to fetch the data from database. Here userEmailFromDB and userPhoneFromDB are the values that you get from database. 

   $scope.userEmail = userEmailFromDB; 
   $scope.userPhone = userPhoneFromDB;
}

【讨论】:

  • 我使用 MySQL 和 PHP 作为后端。如何获取值并将它们分配给 $scope.userEmail 和 $scope.userPhone?
  • 您是否编写了任何 SQL 查询来根据用户获取数据?
  • 是的,我已经在 php 文件中编写了查询
  • 我可以从 php 文件中查看代码吗?函数名是什么,取什么参数,返回什么。
  • 我知道了,我使用 http.get 从我的数据库中获取结果。谢谢大佬。
【解决方案2】:
$scope.userListSelected = function(userList){ $scope.email = userList.email; $scope.phoneNumber = userList.phoneNumber;

<select ng-model="userList" ng-options="user.name for user in userData" ng-change="userListSelected(userList)">

<input type=text ng-model="email" />
<input type=text ng-model="phoneNumber"/>

【讨论】:

    【解决方案3】:

    我有这样的东西,希望对你有帮助:

    $scope.onChangeSelectedUser = function(item) {
      $scope.user.user_id = item.id;
    }
        
    $scope.selectedUser = [];    
    <select class="form-control with-search" select-picker data-live-search="true" title="Select User" ng-model="selectedUser"
    ng-options="u as u.email for u in users"
    ng-change="onChangeSelectedUser(selectedUser)">
    </select>
    
    <input type="text" readonly class="form-control" ng-model="selectedUser.user_id"/>        
    <input type="text" readonly class="form-control" ng-model="selectedUser.email"/>
    <input type="text" readonly class="form-control" ng-model="selectedUser.phone_no"/>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-24
      • 1970-01-01
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      • 2015-07-31
      相关资源
      最近更新 更多