ng-switch不适用于angular-ui typeahead

时间:2014-05-23 09:54:22

标签: javascript angularjs angular-ui ng-switch angular-ui-typeahead

我正在使用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;

1 个答案:

答案 0 :(得分: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替换它