繁体   English   中英

使用数据基于AngularJS来自另一个字段的输入将选项填充到选择中

[英]Populate options into select using data based on input from another field with AngularJS

我有一个带地址的表格。 我要收集的第一个地址字段是一个邮政编码。 输入邮政编码后,一个自定义指令将查询一个外部API,并获取一个包含城市,州,县以及该州所有县列表的对象。

在邮政编码字段上,我具有在分配给城市,州和县输入字段的模型中传递的属性。 该指令采用这些并填充scope变量,以便每个变量的值都同步。

我的问题是在县选择框中。 我首先需要用该州所有县的列表填充该字段(同样,所有可用选项均由API响应提供)。 填充字段后,我需要设置值。

设置很容易。 有用。 我一辈子都想不通如何用可用的选项填充字段。

  • 我不能使用元素ID,因为我需要此指令可用于此格式的多个地址。
  • 我不能使用ng-options,至少在我看来,因为在输入邮政编码之前,我没有要填充的数据。

这是我表单的一小段,显示了邮政编码和县域:

<div class="form--row">
  <div class="form--col5">
    <label class="form--label" for="nbZip">Zip</label>
    <input
      id="nbZip"
      class="form--input"
      type="text"
      ng-model="data.ClientDetails.zip"
      ng-required="data.Client == 'true'"
      blur="update()"
      ziplookup
      zip-city="data.ClientDetails.city"
      zip-state="data.ClientDetails.state"
      zip-county="data.ClientDetails.county"
    >
  </div>

<!-- First Client NB County -->
<div class="form--row">
  <div class="form--col5">
    <label class="form--label" for="nbCounty">County</label>
    <select class="form--input" id="nbCounty" ng-model="data.ClientDetails.county" ng-required="data.Client == 'true'" change="update()">
      <!-- generate county list! -->
    </select>
  </div>
</div>

我的指令:(ZipCode是我注入指令的自定义服务。)

directives.directive(
'ziplookup',
['ZipCode',
function (ZipCode) {
  var scope
    , element
    , zipData;

  var getZipData = function (event) {
    // When this function is called, `this` is
    // bound to the scope matching the element.
    scope = this;

    // We also get access to the element itself.
    element = $(event.target);

    ZipCode
      // Shoot off our call for the location data,
      // using the value in the `<input>`.
      .getLocationData(element.val())

      // When data is returned, we will set the
      // returned data.
      .then(setZipData)

      // Update the view with the returned data.
      .then(updateCity)
      .then(updateState)
      .then(updateCounty)
  };

  var setZipData = function (data) {
    // This function makes the scope and data
    // accessible from the `update___` functions
    // that follow.

    // We'll set `zipData` to the returned data.
    zipData = data.data;
  };

  var updateCity = function () {
    scope.city = zipData.city;
  };

  var updateState = function () {
    scope.state = zipData.state;
  };

  var updateCounty = function () {
    scope.county = zipData.county;
  };

  var link = function (scope, elem) {
    elem.bind('blur', angular.bind(scope, getZipData));
  };

  return {
    restrict: 'A',
    scope: {
      city: '=zipCity',
      state: '=zipState',
      county: '=zipCounty'
    },
    link: link
  };
}]);

您甚至可以在没有可用数据的情况下使用ng-options ,只需在收到API响应后立即填充正确的模型即可。

例如,假设API响应将填充$scope.availableCounties

$scope.availableCounties = ZipCodeAPI.getCounties(zipCode);

然后可以使用它来定义ng-options

<select class="form--input" id="nbCounty" change="update()"
        ng-model="data.ClientDetails.county" ng-required="data.Client == 'true'"
        ng-options="county.id as county.name for county in availableCounties">
</select>

最初它不会显示任何内容,因为$scope.availableCounties将是undefined ,但是一旦您填充了模型(如果一切都正确连接),Angular就会自动将选项添加到<select>字段中。

检查以下示例: http : //jsfiddle.net/bmleite/tD9B4/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM