2012-10-01 52 views
19

我目前正在开发一个web应用程序,它使用twitter-bootstrap和Angularjs协调得很好。但是,我对typeahead有问题,并将其用作ng模型。使用Bootstrap typeahead和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-改变功能只是为了检查当模型值被改变。只有在手动输入时才会更改,而不是在从前面显示的列表中选择一个值时。

我很欣赏任何可能有助于解决此问题的答案。谢谢!

+0

一直这个问题得到了解决? –

+0

不幸的是,没有... –

回答

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 

我有一些问题与数据绑定,自动填充选项从一个角度控制聚集,我有在这些信息准备好之前就已经创建了控件。因此,我在typeahead控件中添加了html-attribute(datasource),并在构造函数中设置了$ observe函数。

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

我认为这是一个肮脏的解决方案,所以如果任何人有更好的主意,我很欢迎听到它:)。这里描述的错误:https://github.com/angular/angular.js/issues/1284

+0

你不需要$(元素).whatever你可以只是element.whatever –

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(); 
}); 
+0

虽然脏,但这听起来最好。 – Hengjie

0

这是基于@ zgohr的实现

$('#your-input-id-here').change((event)-> 
    angular.element("#your-input-id-here").scope().$apply((scope)-> 
    scope.your_ng_model = event.target.value 
) 
) 
21

我建议检查出从AngularUI /自举库中的预输入指令:http://angular-ui.github.com/bootstrap/

它原产在纯AngularJS中实现,所以它不需要任何第三方依赖。除此之外,它与AngularJS生态系统很好地集成在一起,因为它: *使用select指令中已知的简明语法 *了解AngularJS承诺,因此可以使用$http以最小的努力动态获取结果。

+0

你可能会使用角度带,因为它支持最新版本的angularjs;) – genuinefafa

0

另一种选择

在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> 

在控制器

$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 
    } 
9

我做了这个本地预输入实现只在角(对于造型和引导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" /> 
+0

在小提琴上,当你选择它时,它不会将它分配给ngmodel – gdubs

+0

它将item.name赋值给ngModel。获取整个对象,如果这是你的意思,使用回调。或者只是修改它分配你需要的任何东西 – ropsnou