【问题标题】:Populate options into select using data based on input from another field with AngularJS使用 AngularJS 基于来自另一个字段的输入的数据将选项填充到选择中
【发布时间】:2013-02-06 21:48:34
【问题描述】:

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

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

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

设置很简单。有用。我一生都无法弄清楚如何使用可用选项填充该字段。

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

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

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

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    即使没有任何可用数据,您也可以使用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 将自动将选项添加到&lt;select&gt; 字段。

    查看此示例:http://jsfiddle.net/bmleite/tD9B4/

    【讨论】:

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