javascript - AngularJS typeahead 调用 webapi

标签 javascript angularjs typeahead

在此处将 AngularJS 与 C# webapi 结合使用。

我正在创建一个输入控件,当用户开始输入时我想使用预输入并显示返回的数据。

我已经设置了 typeahead 如下:

HTML:

  <input type="text" name="uName" ng-model="uName" autocomplete="off" required class="form-control input-medium" placeholder="Enter user name..."
   typeahead="uName for uName in getUserNames($viewValue)" />

Controller :

    $scope.getUserNames = function (search) {
        myService.getUserNamesFromApi(search).then(function (response) {
            $scope.foundNames = [];
            if (response.length > 0) {
                for (var i = 0; i < response.length; i++) {
                    $scope.foundNames.push({ 'uName': response[i].uName });
                }
                return $scope.foundNames;
            }
        });
    };

从我的 API 返回的数据是一个数组,例如:

0: {fName: "Adam", lName: "Smith", uName: "asmith123"},
1: {fName: "John", lName: "Bambi", uName: "jbambi456"}

等等……

我正在尝试获取 uName 部分并将其推送到我的数组,然后返回该数组。但是目前这段代码没有显示任何内容,没有错误。

我在这里错过了什么?

最佳答案

应该是,

 typeahead="uName as uName.uName for uName in getUserNames($viewValue)" />

关于javascript - AngularJS typeahead 调用 webapi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52319029/

相关文章:

javascript:确保元素始终出现在顶部

javascript - 私有(private)方法和字段

javascript - 在 angularJs 中处理资源和图像超时

php 如果在 javascript 中

javascript - 如何为需要测试其发射的 Angular Directive(指令)进行 Jasmine 单元测试?

javascript - Socket.IO Angular 服务器到服务器连接

javascript - 如何使用加减值验证文本框 angularjs

javascript - 使用 Ionic/AngularJS 和 Cordova 为 native 和 Web 应用程序使用相机

jquery - Bootstrap 提前输入 : show different text in box once selected

javascript - 为什么这个 Angular-ui TypeAhead 代码返回所有项目而不是过滤的项目?