具有发布数据的Angular Typeahead

时间:2015-09-29 11:33:50

标签: angularjs typeahead

我想在angular中实现typeahead。出于某些原因,我想通过POST方法实现它,而不是通过GET方法传递params。以下代码有什么问题?

HTML:

    <div ng-controller="TypeaheadCtrl">
        <h4>Asynchronous results</h4>
        <pre>Model: {{asyncSelected | json}}</pre>
        <input type="text" ng-model="asyncSelected" placeholder="Data loaded via $http" typeahead="name for name in getNames($viewValue)" typeahead-loading="loadingNames" typeahead-no-results="noResults" class="form-control"/>
        <i ng-show="loadingNames" class="glyphicon glyphicon-refresh"></i>
        <div ng-show="noResults">
            <i class="glyphicon glyphicon-remove"></i>
            No Results Found
        </div>
    </div>

JS :( XQuery是服务)

    myApp.controller('TypeaheadCtrl', function($scope, $http) {

        $scope.getNames = function(val) {

            $http({
                method  : 'POST',
                url     :  './myResource?workbench/APP16/service/myservice.xq',
                data    :  '<data><name>'+val+'</name></data>',
                headers : { 'Content-Type': 'application/xml' }
            })
            .sucess(function(response)
            {  
                return( response.hint.map(function(item){
                return item.text;
                }));
            });
        };

    });

响应数据:

    {
        "hint": [
            {
                "pattern": "clinicInfo/name/Rudvik",
                "text": "Rudvik",
                "type": "clinicProfile",
                "image": null
            },
            {
                "pattern": "clinicInfo/name/raju",
                "text": "raju",
                "type": "clinicProfile",
                "image": null
            }
        ]
    }

当我在返回数据上使用console.log时,

    console.log( response.hint.map(function(item){
                return item.text;
                }));

我得到了:[Rudvik,raja]

但是在打字机中我什么都没得到。怎么做?

1 个答案:

答案 0 :(得分:0)

你几乎就在那里,你忘了return函数中的getNames

替换:

$scope.getNames = function(val) {
    $http(...).sucess(function(response) {  
            // ...
    });
};

使用:

$scope.getNames = function(val) {
    return $http(...).sucess(function(response) {  
            // ...
    });
};