[AngularJS]未捕获 TypeError︰ 无法读取属性 indexOf 的定义在 angularjs

发布时间: 2017/4/16 21:33:33
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我是很新的编码,世界,需要一些帮助从专家。下面是我注意到在文件中的 angularJs 错误︰ 错误指向这︰

 if( searchItemsSmallLetters.indexOf(searchTextSmallLetters) !== -1){

您的协助,将不胜感谢。 我试图给用户建议基于的密钥的用户输入。

var app = angular.module('app',[]);
app.controller('autoCompleteCTRL', function($scope, $rootScope,$http){
      $rootScope.searchItems  = [];
     var arr=  getCountries(); // Load all countries with capitals
  function getCountries(){  
  $http.get("ajax/getCountries.php").success(function(data){
          for(var i=0;i<data.length;i++)
        {
             $rootScope.searchItems.push(data[i].bvl_area);
        }

      return $rootScope.searchItems;
       });
  };
    //Sort Array
    $rootScope.searchItems.sort();
    //Define Suggestions List
    $rootScope.suggestions = [];
    //Define Selected Suggestion Item
    $rootScope.selectedIndex = -1;

    //Function To Call On ng-change
    $rootScope.search = function(){
        $rootScope.suggestions = [];
        var myMaxSuggestionListLength = 0;
        for(var i=0; i<$rootScope.searchItems.length; i++){
            var searchItemsSmallLetters = angular.lowercase($rootScope.searchItems[i]);
            var searchTextSmallLetters = angular.lowercase($scope.searchText);
            if( searchItemsSmallLetters.indexOf(searchTextSmallLetters) !== -1){
                $rootScope.suggestions.push(searchItemsSmallLetters);
                myMaxSuggestionListLength += 1;
                if(myMaxSuggestionListLength == 5){
                    break;
                }
            }
        }
    }

    //Keep Track Of Search Text Value During The Selection From The Suggestions List  
    $rootScope.$watch('selectedIndex',function(val){
        if(val !== -1) {
            $scope.searchText = $rootScope.suggestions[$rootScope.selectedIndex];
        }
    });


    //Text Field Events
    //Function To Call on ng-keydown
    $rootScope.checkKeyDown = function(event){
        if(event.keyCode === 40){//down key, increment selectedIndex
            event.preventDefault();
            if($rootScope.selectedIndex+1 !== $rootScope.suggestions.length){
                $rootScope.selectedIndex++;
            }
        }else if(event.keyCode === 38){ //up key, decrement selectedIndex
            event.preventDefault();
            if($rootScope.selectedIndex-1 !== -1){
                $rootScope.selectedIndex--;
            }
        }else if(event.keyCode === 13){ //enter key, empty suggestions array
            event.preventDefault();
            $rootScope.suggestions = [];
        }
    }
    //Function To Call on ng-keyup
    $rootScope.checkKeyUp = function(event){ 
        if(event.keyCode !== 8 || event.keyCode !== 46){//delete or backspace
            if($scope.searchText == ""){
                $rootScope.suggestions = [];
            }
        }
    }
    //======================================

    //List Item Events
    //Function To Call on ng-click
    $rootScope.AssignValueAndHide = function(index){
         $scope.searchText = $rootScope.suggestions[index];
         $rootScope.suggestions=[];
    }
    //======================================

});

解决方法 1:

SearchTextSmallLetters变量的值已经变得不确定。首先你检查是否它可以是未定义的。如果是这样将行更改为

if(searchItemsSmallLetters && searchItemsSmallLetters.indexOf(searchTextSmallLetters) !== -1){

否则检查工作上"ajax/getCountries.php"的服务,并返回预期的输出。因为您使用 get 请求可以使用浏览器来做那 (%domain%/ajax/getCountries.php eg:-localhost:8080/ajax/getCountries.php)

重要︰ 它总是好做验证。所以它是好将行更改为

if(searchItemsSmallLetters && searchItemsSmallLetters.indexOf(searchTextSmallLetters) !== -1){
赞助商