【问题标题】:AngularJS Typeahead on non selectionAngularJS Typeahead 非选择
【发布时间】:2014-06-13 14:15:42
【问题描述】:

我添加了 Angular Bootstrap 的 Typeahead。它工作正常,我想为其添加一项功能。 So when an item is selected I want to toggle the disability of other components only if something is selected in Typeahead.问题是,如果我在第一个 Typeahead 中输入了一些内容并且我没有选择任何内容,它不会触发任何事件。

这是组件的 html。我想问的是我应该听哪个事件来实现这个功能?

<div class="form-group has-feedback" ng-controller="CitiesSearchController">
            <input type="text" id="cityCombo" ng-model="selectedCityName" placeholder="City"
                   data-typeahead="cities.CityName for cities in getCities($viewValue)"
                   data-typeahead-loading="loadingCities"
                   data-typeahead-min-length="1"
                   data-typeahead-wait-ms="0"
                   data-typeahead-on-select="onCitySelect($item, $model, $label)"
                   class="form-control" />
            <span ng-show="loadingCities" class="glyphicon glyphicon-refresh form-control-feedback"></span>
        </div>

我在想的是:我可以触发一个 ng-changed 事件。因此,每当更改文本时,它都会触发和事件,并在此基础上禁用其他组件。这是正确的方法吗?

【问题讨论】:

  • 你能再解释一下吗?当用户没有从列表中选择时,您期望会发生什么?
  • 每当编辑文本时(即未进行选择时),我希望禁用其他组件。所以在这里我选择城市,所以除非选择一个城市,否则我希望禁用区域框。当然,只有在选择城市时才能填充。

标签: angularjs


【解决方案1】:

您可以通过在selectedCityName 上调用$watch 来订阅您的ng-model 值的更改:

在这里工作的 Plunker: http://plnkr.co/edit/sieXxn?p=preview

  $scope.$watch("selectedCityName", function(value) {

      $scope.isDisabled = value ? true : false;

  });

这里要注意的一件非常重要的事情是,在我的示例中,我设置了typeahead-editable="false" 以防止无效选择并将它们限制为原始数组中的选择。因此,除非做出有效选择,否则 $watch 事件不会触发。您当然可以将其更改为 true,但这将允许在您的 ng-model 中输入任何我认为无效的虚拟输入。

我希望这会有所帮助。

【讨论】:

  • 请参阅上面我对 John C. 的评论。
  • 为什么不反过来呢?禁用控件并仅在有效选择时启用它们?
  • @DianaNassar 如果用户做出了有效的选择,然后决定继续输入,则将启用控件。听起来 huber.duber 希望在用户输入时禁用所有内容。
  • @JoshC。是的,但他不能点击任何东西。如果他输入无效值,他们将立即被禁用;因此,即使它们确实启用了,它们也会暂时启用,并且不会产生任何影响,因为用户将无法单击/输入任何内容。
  • 如果我将上述语句切换为: $scope.isDisabled = value ,您的解决方案对我的场景有效假:真;
猜你喜欢
  • 1970-01-01
  • 2014-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-27
  • 2014-11-21
  • 1970-01-01
相关资源
最近更新 更多