typeahead没有显示bootstrap UI和angularjs的建议

时间:2016-05-24 08:15:58

标签: javascript angularjs angular-ui-bootstrap

我想在搜索文本框中显示静态建议但不显示任何内容。我正在关注这个https://codepen.io/joe-watkins/pen/EagEWv。 这是我的代码。

HTML code:

 <div>    
    <input type="text" name="questions" id="questionss" class="form-control select2-search search_query" placeholder="what are you looking for ?" ng-model='myquery' typeahead="question for question in questions | filter:$viewValue | limitTo:3" />

    <div class="form-group">
        <button type="button" class="btn btn-primary" ng-click="search()">
          Get an answer ! <i class="fa fa-search"></i>
        </button> 
    </div>

我的控制员:

var Questions=["hello","hi","question1","question2","question2"];
$scope.questions=Questions;

1 个答案:

答案 0 :(得分:1)

检查浏览器控制台是否有任何错误,因为您的代码确实正常工作。

1.确保您已包含所有依赖项:

<link rel="stylesheet prefetch" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.12.0.js"></script>

2.在html中注明ng-app="angularTypeahead"并且:

var myApp = angular.module("angularTypeahead", ["ui.bootstrap"]);

3.Notice ng-controller="TypeaheadCtrl"和:

myApp.controller("TypeaheadCtrl", function($scope) {...});

4.完整代码:

这是 full code live demo

&#13;
&#13;
<html>
    <head>
      <link rel="stylesheet prefetch" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
      <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
      <script src="//angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.12.0.js"></script>
    </head>
    
    <body>
      <div ng-app="angularTypeahead">
        <div class="container-fluid" ng-controller="TypeaheadCtrl">
    
          <label for="states">Search for Answer</label>
          <input type="text" name="questions" id="questionss" class="form-control select2-search search_query" placeholder="what are you looking for ?" ng-model='myquery' typeahead="question for question in questions | filter:$viewValue | limitTo:3">
    
          <div class="form-group">
            <button type="button" class="btn btn-primary" ng-click="search()">
              Get an answer ! <i class="fa fa-search"></i>
            </button>
          </div>
    
    
        </div>
      </div>
      <script type="text/javascript">
        var myApp = angular.module("angularTypeahead", ["ui.bootstrap"]);
    
    
        // setup controller and pass data source
        myApp.controller("TypeaheadCtrl", function($scope) {
    
          var Questions = ["hello", "hi", "question1", "question2", "question2"];
          $scope.questions = Questions;
    
        });
      </script>
    </body>
    
    </html>
&#13;
&#13;
&#13;