在angularjs中使用自定义指令实现自动建议

时间:2018-09-05 11:41:27

标签: angularjs web angularjs-directive angularjs-controller

我试图实现自定义指令并在无序列表中显示,但是应该保存过滤后的名称的数组却无法这样做。 这是我的指令 我需要过滤输入数据并从主数组中获取所有匹配的名称

app.directive('autoComplete', function() {     
    function link(scope, iElement, iAttrs,modelCtrl) {              
        //click event handling
        scope.fillTextbox = function(name){ 
            //modelCtrl.$setViewValue(name);
            //scope.filtered = scope[iAttrs.uiItems].filter(function(item){
            //return item.includes(inputValue);
            //      });
        }
        iElement.bind("keydown", function (event) {
            if(!scope.filtered.length){
                return;
            }               
            if(event.which == 38){                      //keyup
                if(scope.focusIndex > 0){
                    scope.$apply(function (){
                        scope.focusIndex-= 1;
                        scope.selected = scope.filtered[scope.focusIndex]; ////

                    });

                }else{event.preventDefault();}
                return;
            }else if(event.which == 40){                        //keydown
                if(scope.focusIndex < scope.filtered.length-1){
                    scope.$apply(function (){
                        scope.focusIndex+=1;
                        scope.selected = scope.filtered[scope.focusIndex]; ///
                    }); 
                }else{event.preventDefault();}
            }
            else if(event.which == 13){                     //enter                 
                scope.$apply(function (){
                    scope.selected = scope.filtered[scope.focusIndex];
                    console.log(scope.filtered[scope.focusIndex]);
                    scope.focusIndex = 0;
                    scope.filtered = [];
                });
            }
        });    
    }
    return {
        restrict: 'A',
        template :`<ul ng-model="listofnames" ng-show="true">
                       <li  class="list-group-item"
                            ng-repeat="name in filtered"
                            ng-click="fillTextbox(name)">{{name}}
                       </li>
                   </ul>`,
        require: 'ngModel',
        scope : {filtered : '='},
        link : link
    }  
});

这是我的控制者。 changeInText()函数不起作用,并且名称也没有从名称数组中过滤出来。

app.controller("DefaultCtrl",["$scope" , function($scope){
    $scope.names = ["john", "john1", "john2", "john3", "bill", "charlie",
        "robert", "alban", "oscar", "marie", "celine", "brad", "drew",
        "rebecca", "michel", "francis", "jean", "paul", "pierre", "nicolas", 
        "alfred", "gerard", "louis", "albert",
        "edouard", "benoit", "guillaume", "joseph"];
    $scope.foundname = [];
    $scope.filtered = [];
    $scope.focusIndex = 0;  
    $scope.changeInText = function (){
        for(var i = 0; i< $scope.names.length ; i++){
            if($scope.names[i].includes($scope.selected)){
                $scope.filtered.push($scope.names[i]);
            }   
        }
        console.log($scope.filtered);
        $scope.filtered=[];                 
    }
}]);

0 个答案:

没有答案