首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >typeahead从url key和api angularjs获取数据

typeahead从url key和api angularjs获取数据
EN

Stack Overflow用户
提问于 2016-06-27 13:41:24
回答 3查看 258关注 0票数 1

嗨,我是Angularjs的新手。我正在尝试创建一个typeahead,其中我通过api获取数据。我试着寻找解决方案,但我找不到我想要的解决方案。下面是我到目前为止所做的代码。HTML代码:

代码语言:javascript
复制
<div ng-controller="newController">
    <div class="selection-box">
        <div class="title-box">
            <div class="search_item">
                <input name="document" ng-model='query' type="text" typeahead="document as document.name for document in documents | filter:query | limitTo:8" id='document' placeholder="SEARCH FOR YOUR DOCUMENT" class="search_box">
            </div>
            {{query}}
        </div>
    </div>
</div>

在此输入框中,无论我输入什么,都会在{{query}}中打印出来,但不会显示任何从api获取的数据。我正在使用bootstrap ui。下面是我写的控制器。

newController.js:

代码语言:javascript
复制
var myApp = angular.module('myModule', ['ui.bootstrap']);

myApp.service("searchService", function ($http) {
     var apiUrl = "http://12.56.677/api/v1/mobile/";
     var apiKey = "123nm";
     this.searchDocument = function(query) {
         var response = $http({
         method: 'post',
         url: apiUrl + "search",
         headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
         params: {
           key: apiKey,
           query: query
         }
      });
      return response;
    };
});

myApp.controller('newController', ['$scope', 'searchService' , function($scope, searchService, $rootScope, $http, $window, $document) {
    var apiUrl = "http://12.56.677/api/v1/mobile/";
    var apiKey = "123nm";
    url = apiUrl + "search";
    Key = apiKey;
    $scope.query  = undefined;
    console.log(query);
    searchService.searchDocument(query='').then (function (res) {
    if(res.data.status == "OK")
    {

        $scope.documents = res.data.result;
        console.log($scope.documents);
        // var userinformation = res.data.result;
        // $window.localStorageService.setItem('searchDocument',      JSON.stringify(query));
     }
     else {

         $scope.errorMessage = res.data.message;
     }
  })
 }])

任何帮助都将不胜感激。

EN

回答 3

Stack Overflow用户

发布于 2016-06-27 13:54:15

代码语言:javascript
复制
  //HTML
  <input name="document" 
   ng-model='query' type="text" 
   uib-typeahead="document as document.name 
   for document in documents  |filter:query | limitTo:8" id='document' 
   placeholder="SEARCH FOR YOUR DOCUMENT" class="search_box">

  //Controller
  searchService.searchDocument('').then (function (res) {
    if(res.data.status == "OK"){
      $scope.documents = res.data.result;
    }
    else {
     $scope.errorMessage = res.data.message;
    }
  });
票数 0
EN

Stack Overflow用户

发布于 2016-06-27 13:55:35

您尝试做的是使用带有预取列表的typeahead (带有空查询)。

您可能想要执行异步搜索,并且需要一个数据获取函数来执行此操作。

HTML,请注意typeahead表达式:

代码语言:javascript
复制
<input name="document" ng-model='query' type="text" 
       typeahead="document as document.name for document in (getDocuments() | limitTo:8)" 
       id='document' 
       placeholder="SEARCH FOR YOUR DOCUMENT" class="search_box">

控制器:

代码语言:javascript
复制
myApp.controller('newController', ['$scope', 'searchService' , function($scope, searchService, $rootScope, $http, $window, $document) {
    var apiUrl = "http://12.56.677/api/v1/mobile/";
    var apiKey = "123nm";

    // What are these two undeclared variables lying here around for?
    url = apiUrl + "search";
    Key = apiKey;

    $scope.getDocuments = function(query){
        searchService.searchDocument(query) // Check your API, I am not sure how you're API treats these parameters
            .then (function (res) {
                if(res.data.status == "OK")
                {
                    var documents = res.data.result;
                    console.log(documents);
                    return documents;
                }
                else {
                    $scope.errorMessage = res.data.message;
                    return [];
                }
            })
    }
}])
票数 0
EN

Stack Overflow用户

发布于 2016-06-27 14:51:42

我认为您需要从searchService.searchDocment()返回promise,如下所示:

代码语言:javascript
复制
return searchService.searchDocument(query='').then(......)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38046599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档