20

我目前正在开发一个 Web 应用程序,它使用 twitter-bootstrap 和 Angularjs 很好地协调。但是,我在使用预输入并将其用作 ng 模型时遇到了问题。

键入时一切正常,但是当我选择一个项目(建议)时,该值不会反映在 Angular 控制器中,除非我在选择一个值后更改文本框的值。类型 -> 选择 -> 类型有效。类型 -> 选择不起作用。

HTML:

<form ng-submit="addAssignment(assignName)">
  <div class="input-append">
    <input type="text" placeholder="Type a name" ng-model="assignName" ng-change="dostuff()" data-provide="typeahead" data-source="{{ teamNames }}">
    <button class="btn" type="submit">Add</button>
 </div>
</form>

角代码:

 $scope.addAssignment = function(name) {
    alert(name);
    return;
 }

我添加了一个 ng-change 函数,只是为了检查模型值何时更改。它仅在手动键入时更改,而不是在从出现在 typeahead 的列表中选择一个值时更改。

我感谢任何可能有助于解决此问题的回复。谢谢!

4

7 回答 7

22

我建议从 AngularUI/boostrap 存储库中检查 typeahead 指令:http: //angular-ui.github.com/bootstrap/

它是纯 AngularJS 中的本机实现,因此不需要任何 3rd 方依赖项。最重要的是,它很好地与 AngularJS 生态系统集成,因为它: * 使用select指令中已知的简洁语法 * 理解 AngularJS 承诺,因此可以以$http最小的努力动态获取结果。

于 2013-03-25T13:23:10.823 回答
21

AngularStrap中有一个用于 Bootstrap3 的本地实现,它利用ngAnimate了 AngularJS v1.2+

您可能还想结帐:

于 2012-12-26T02:34:59.963 回答
9

我制作了这个原生的 typeahead 实现,只依赖于 angular (和引导 css 的样式),可能会帮助任何人寻找如何做到这一点。

在这里演示:https ://jsfiddle.net/bh29tesc/

角度指令:

angular.module('app').
directive('typeahead', ['$compile', '$timeout', function($compile, $timeout)   {
    return {
        restrict: 'A',
        transclude: true,
        scope: {
            ngModel: '=',
            typeahead: '=',
            typeaheadCallback: "="
        },
        link: function(scope, elem, attrs) {
            var template = '<div class="dropdown"><ul class="dropdown-menu" style="display:block;" ng-hide="!ngModel.length || !filitered.length || selected"><li ng-repeat="item in filitered = (typeahead | filter:{name:ngModel} | limitTo:5) track by $index" ng-click="click(item)" style="cursor:pointer" ng-class="{active:$index==active}" ng-mouseenter="mouseenter($index)"><a>{{item.name}}</a></li></ul></div>'

            elem.bind('blur', function() {
                $timeout(function() {
                    scope.selected = true
                }, 100)
            })

            elem.bind("keydown", function($event) {
                if($event.keyCode == 38 && scope.active > 0) { // arrow up
                    scope.active--
                    scope.$digest()
                } else if($event.keyCode == 40 && scope.active < scope.filitered.length - 1) { // arrow down
                    scope.active++
                    scope.$digest()
                } else if($event.keyCode == 13) { // enter
                    scope.$apply(function() {
                        scope.click(scope.filitered[scope.active])
                    })
                }
            })

            scope.click = function(item) {
                scope.ngModel = item.name
                scope.selected = item
                if(scope.typeaheadCallback) {
                    scope.typeaheadCallback(item)
                }
                elem[0].blur()
            }

            scope.mouseenter = function($index) {
                scope.active = $index
            }

            scope.$watch('ngModel', function(input) {
                if(scope.selected && scope.selected.name == input) {
                    return
                }

                scope.active = 0
                scope.selected = false

                // if we have an exact match and there is only one item in the list, automatically select it
                if(input && scope.filitered.length == 1 && scope.filitered[0].name.toLowerCase() == input.toLowerCase()) {
                    scope.click(scope.filitered[0])
                }
            })

            elem.after($compile(template)(scope))
        }
    }
}]);

用法:

<input class="form-control" type="text" autocomplete="false" ng-model="input" placeholder="Start typing a country" typeahead="countries" typeahead-callback="callback" />
于 2015-06-19T18:56:23.927 回答
3

好吧,我创建了一个肮脏的解决方法。根据此处的示例:https ://groups.google.com/forum/#!topic /angular/FqIqrs-IR0w/discussion,我为预输入控件创建了一个新模块:

angular.module('storageApp', []).directive('typeahead', function () {
return {
    restrict:'E',
    replace:true,
    scope:{
        model:'=',
        source:'&'
    },
    template:'<input type="text" ng-model="model"/>',
    link:function (scope, element, attrs) {
        console.log(scope.source);
        $(element).typeahead({
            source:scope.source,
            updater:function (item) {
                scope.$apply(read(item));
                return item;
            }
        });

        function read(value) {
            scope.model = value;
        }
    } // end link function
}; // end return
}); // end angular function

我在数据绑定方面遇到了一些问题,自动填充选项是从 Angular 控件中收集的,并且我遇到的问题是控件是在此信息准备好之前创建的。因此,我在 typeahead 控件中添加了一个 html 属性(数据源),并在构造函数中设置了一个 $observe 函数。

<typeahead id="addSupplier" model="addSupplier" placeholder="Skriv inn leverandør" class="typeahead" source="getSuppliers()" ></typeahead>

我认为这是一个肮脏的解决方案,所以如果有人有更好的想法,我很高兴听到它:)。该错误在此处描述:https ://github.com/angular/angular.js/issues/1284

于 2012-10-08T15:54:14.697 回答
0

Another alternative

In HTML

    <form ng-submit="submitRegion()">
        <input type="text" ng-model="currentRegion" id="region-typeahead" data-source="{{ defaultRegions }}"  data-provide="typeahead"/>
        <button type="submit" class="btn">Add</button>
    </form>

In your Controller

    $scope.defaultRegions = ["Afghanistan", "Australia", "Bahrain", "New Zealand" ];

    $scope.submitRegion = function(){
        $scope.currentRegion = $('#region-typeahead').val();
        $scope.addRegion(); //your add or click function you defined
        $scope.currentRegion = ''; //clear
    }
于 2013-05-28T06:32:36.000 回答
0

这是基于@zgohr 的实现

$('#your-input-id-here').change((event)->
  angular.element("#your-input-id-here").scope().$apply((scope)->
    scope.your_ng_model = event.target.value
  )
)
于 2013-03-04T08:53:27.147 回答
0

这是我使用的另一种方法。它也很脏。此代码可以放在您的控制器中。

$('#id_of_your_typeahead_input').change(function(event){
  $scope.$apply(function(scope){
    scope.your_ng_model = event.target.value;
  });
  $scope.your_ng_click_function();
});
于 2012-12-11T20:01:08.737 回答