AngularJS从Select中设置输入

时间:2014-08-20 00:44:55

标签: angularjs select input selected

您如何在选择中填充数据并填充输入表单?

到目前为止

代码:

<form class="form-horizontal well" role="form" novalidate name="editSomeForm" ng-init="getSomeNames()">
  <div class="form-group">
    <label for="name" class="col-sm-3 control-label">Some Name: </label>
    <div class="col-sm-9">
      <select class="form-control" id="name" ng-model="some.name" ng-options="s.name for s in data.some_names"></select>
    </div>
  </div>
  <div class="form-group">
    <label for="someId" class="col-sm-3 control-label">Some ID: </label>
    <div class="col-sm-9">
      <input type="text" class="form-control" id="someId" name="someId" ng-model="some.id"/>
    </div>
  </div>

JS:

app.controller('EditSomething', function($scope, $http) {
  $scope.getSomeNames = function() {
    $http.get('/api/something/editinfo').
      success(function (data) {
        $scope.some.name = data.some_names[0];
        $scope.data = data;
      }).
      error(function (data){
        $scope.some_names = {}
      });
  };

1 个答案:

答案 0 :(得分:0)

只需将输入字段的ng-model绑定为与select字段相同,但请记住这是“双向”。如果更改输入字段,它将尝试更改选择字段。

另一种方法是添加ng-change属性并激活一个范围函数,将数据分配给另一个范围变量