简体   繁体   English

清除Angular UI Bootstrap typeahead上的$ viewValue

[英]Clearing the $viewValue on Angular UI Bootstrap typeahead

I have a form with fields that are dependent on each other, displayed using UI Bootstrap . 我有一个表单,其中的字段相互依赖,使用UI Bootstrap显示。 Selecting a team from a bootstrap typeahead then populates a list of players that can then be selected in a second typeahead. 从bootstrap typeahead中选择一个团队,然后填充一个玩家列表,然后可以在第二个类型中选择。 When the user changes the team I reload the list of players and clear the currently selected player value. 当用户更改团队时,我会重新加载播放器列表并清除当前选定的播放器值。

The player options are loaded correctly based on the team, but the issue is that the typeahead is not showing the options when clicked on - I think I might be running afoul of the $viewValue being in a weird state and not matching any of the existing options. 玩家选项是根据团队正确加载的,但问题是当点击时类型未显示选项 - 我想我可能会因为$ viewValue处于奇怪的状态并且没有匹配任何现有的选项。 If I type a letter that happens to match one of my unseen choices then I get the matching values, and clearing anything that has been typed restores the full list. 如果我输入的字母恰好与我看不见的选项相匹配,那么我会得到匹配的值,并清除已键入的内容会恢复完整列表。

I would like the full list to be available on the first click - I suspect I need to reset the $viewValue properly, and I have toyed with trying $setViewValue and $render() on the player model, but have not gotten anywhere. 我希望第一次点击时可以使用完整的列表 - 我怀疑我需要正确地重置$ viewValue,并且玩家在玩家模型上尝试$setViewValue$render()但是没有得到任何结果。

<div class="options">
Team: <input type="text" 
          name="team"
          placeholder="Select team"
          ng-model="selectedTeam" 
          uib-typeahead="team as team.name for team in league | filter:$viewValue" 
          typeahead-editable="false"
          typeahead-min-length="0"
          class="form-control">
</div>

<div class="options">
<input type="text" 
        name="player"
        placeholder="Select player"
        ng-model="selectedPlayer" 
        uib-typeahead="player as player.name for player in players | filter:$viewValue" 
        typeahead-editable="false"
        typeahead-min-length="0"
        class="form-control">
</div>

The controller that looks for a team change and then resets the player options and selected value: 寻找团队变化的控制器然后重置播放器选项和所选值:

$scope.$watch('selectedTeam.id', function(newTeamID) {

    // clears any currently selected player ng-model
    $scope.selectedPlayer = null;

    if (!newTeamID)
    {
      return;
    }

    // sets up the list of available players
    $scope.pullPlayers(newTeamID);

});

Full plunkr is available here . 这里有完整的plunkr。

To reset players typeahead value from controller you might want something like this: 要从控制器重置players预先输入值,您可能需要以下内容:

 $scope.$watch('selectedTeam.id', function(newTeamID) {
    if (!newTeamID)
    {
       $scope.players = [];
       $scope.teamForm.player.$setViewValue('');
       $scope.teamForm.player.$render();
       return;
    }

    // sets up the list of available players
    $scope.pullPlayers(newTeamID);
  });

Here is a forked plunker 这是一个分叉的plunker

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

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