typeahead无法从api angularjs获取数据

时间:2021-02-04 19:37:54

this is my html code

这是我的HTML代码

<input type="text" ng-model="ngModelOptionsSelected" 
 placeholder="Enter Story Title"
 ng-model-options="modelOptions" 
 ng-change="onChangeFunction()"
 typeahead-on-select="typeaheadOnSelectFunction()"
 uib-typeahead="document as document.Name for document in getStory($viewValue)" 
 class="form-control">

this is my .js code

这是我的.js代码

$scope.getStory = function (val) {
storyService.GetStoryByName(cacheService.project.projectId,val).success(function (data) {
                if (data.ResponseStatus) {
               debugger;
                    return  data.ResponseData;
                } else {
                    //On failure
                    toastr.error(data.ErrorData.Error);
                }
            });
        };

function output which returns data like ResponseData =

函数输出,返回ResponseData =等数据

[{"Id":211380.0,"Name":"dixit"},{"Id":211488.0,"Name":"dixit ade"},{"Id":251541.0,"Name":"dixit"},{"Id":842671.0,"Name":"dixit"},{"Id":842672.0,"Name":"dixit choksi"}]

but i am not able to bind data in typeahead.

但我无法在typeahead中绑定数据。

please help me i am stuck. thanks

请帮助我,我被卡住了。谢谢

1 个解决方案

#1


1  

Your function $scope.getStory() doesn't actually return anything, your return line return data.ResponseData; is nested within another function.

你的函数$ scope.getStory()实际上没有返回任何东西,你的返回行返回data.ResponseData;嵌套在另一个函数中。

The uib-typeahead directive is able to work with promises so you just need to return the promise from your function.

uib-typeahead指令能够使用promises,因此您只需要从函数中返回promise。

$scope.getStory = function (val)
{
    return storyService.GetStoryByName(cacheService.project.projectId, val).success(function (data)
    {
        if (data.ResponseStatus)
            return data.ResponseData;

        toastr.error(data.ErrorData.Error);

        return [];
    });
};

You can also add another property to the directive, typeahead-loading="isLoading", that will toggle whilst the promise resolves. It can be used to show/hide loading spinners for example!

您还可以向指令添加另一个属性,即typeahead-loading =“isLoading”,它将在promise解析时切换。例如,它可用于显示/隐藏加载微调器!

#1


1  

Your function $scope.getStory() doesn't actually return anything, your return line return data.ResponseData; is nested within another function.

你的函数$ scope.getStory()实际上没有返回任何东西,你的返回行返回data.ResponseData;嵌套在另一个函数中。

The uib-typeahead directive is able to work with promises so you just need to return the promise from your function.

uib-typeahead指令能够使用promises,因此您只需要从函数中返回promise。

$scope.getStory = function (val)
{
    return storyService.GetStoryByName(cacheService.project.projectId, val).success(function (data)
    {
        if (data.ResponseStatus)
            return data.ResponseData;

        toastr.error(data.ErrorData.Error);

        return [];
    });
};

You can also add another property to the directive, typeahead-loading="isLoading", that will toggle whilst the promise resolves. It can be used to show/hide loading spinners for example!

您还可以向指令添加另一个属性,即typeahead-loading =“isLoading”,它将在promise解析时切换。例如,它可用于显示/隐藏加载微调器!