Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了以编程方式选择AngularJS Typeahead选项大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个AngularJS Typeahead,它可以异步检索匹配项.当条形码扫描到字段中时,它会返回匹配结果,但用户仍然必须选择它.如果它完全匹配,我想自动选择结果.我看到typeahead有一个SELEct(idX)函数,但我不确定如何从我的控制器获取它的引用.

我想象的是这样的

$scope.SearchItems = function (term) {
    return $http.get('api/Items/Search',{
        params: {
            term: term
        }
    }).then(function (responsE) {
        if (response.data.length == 1 && response.data[0].Code == term) {
            // Somehow inform typeahead control to SELEct response.data[0]
        }
        return response.data;
    });
};

解决方法

我有一个类似的问题,从来没有想过如何访问typeahead的选择(idX),但我设法让这个功能工作.这是我的hacky解决方法….

$promise.then(function(res) {
 angular.forEach(res,function(item,key) {

     // if we have an exact match
     if (item.title === term) {
         // update model
         $scope.src = item;

         // find item in dropdown
         var elm = '[id*=option-' + key + ']';
         var opt = angular.element(document.querySELEctorAll(elm));

         //call click handler outside of digest loop
         $timeout(function() {
             opt.triggerHandler('click');
         },0);
     }
 });
 // return async results
 return res;
});

基本上我们只是手动更新我们的模型,在我们的下拉列表中找到元素,然后触发’click’处理程序.确保将$timeout()中的triggerHandler调用设置为零,否则您将获得$rootScope:inprog错误,因为@L_772_14@已在进行中.

大佬总结

以上是大佬教程为你收集整理的以编程方式选择AngularJS Typeahead选项全部内容,希望文章能够帮你解决以编程方式选择AngularJS Typeahead选项所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。