1

我正在使用 angular-ui typeahead 指令和 ng-switch。目的是在输入预输入框时,会出现以“人员信息”开头的行。否则,它不会出现。现在的问题是这一行总是出现。

我在这里创建了一个 plunker。 http://plnkr.co/edit/qEaYE6BHjmMewmlheFoh

相关代码段如下;HTML;

<div class='container-fluid' ng-controller="TypeaheadCtrl">
        <h4>Static arrays</h4>
        <!-- <pre>Model: {{selected }}</pre>
        <pre>Model: {{selected.name}} {{selected.id}}</pre> -->
        <div ng-switch="selected.name">
            <div ng-switch-when="" > 
                <BR><BR>                
            </div>
            <div ng-switch-default >              
                <pre>Person info: {{selected.name}} {{selected.id}}</pre> 
            </div>
        </div> 

        <input type="text" ng-model="selected" 
               typeahead="person as person.name for person in person_info | filter:{name:$viewValue} | limitTo:8"                
               class="form-control">        
    </div>

Javascript控制器:

var person_info =  [
                        {
                                "name": "Tom",
                                "id": "111"
                        },
                        {
                                "name": "Sam",
                                "id": "222"
                        },
                        {
                                "name": "James",
                                "id": "333"
                        }
                    ];
    $scope.person_info = person_info;
4

1 回答 1

2

您可以使用 ng-if/ng-show/ng-hide 而不是 ng-switch

这是带有 ng-if 的代码

 <div ng-if="selected.name == null" > 
      <BR><BR>                
 </div>
 <div ng-if="selected.name">              
      <pre>Person info: {{selected.name}} {{selected.id}}</pre> 
 </div>

只需将其替换为 ng-switch

于 2014-05-23T10:04:52.373 回答