Javascript 未捕获类型错误:无法读取属性';指数';在angularjs中未定义的
我对编码领域非常陌生,需要专家的帮助。以下是我在文件中注意到的angularJs错误: 错误指向以下内容:Javascript 未捕获类型错误:无法读取属性';指数';在angularjs中未定义的,javascript,php,angularjs,Javascript,Php,Angularjs,我对编码领域非常陌生,需要专家的帮助。以下是我在文件中注意到的angularJs错误: 错误指向以下内容: if( searchItemsSmallLetters.indexOf(searchTextSmallLetters) !== -1){ 非常感谢您的帮助。 我试图根据用户输入的密钥给出用户建议 var app = angular.module('app',[]); app.controller('autoCompleteCTRL', function($scope, $rootSco
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=[];
}
//======================================
});
var-app=angular.module('app',[]);
app.controller('autocomplettectrl',函数($scope、$rootScope、$http){
$rootScope.searchItems=[];
var arr=getCountries();//用资本加载所有国家/地区
函数getCountries(){
$http.get(“ajax/getCountries.php”).success(函数(数据){
对于(var i=0;i更改下一行以进行适当处理
if( searchItemsSmallLetters.indexOf(searchTextSmallLetters) !== -1){
到
当“searchItemsSmallLetters”未定义时,您的条件将引发此类错误。searchTextSmallLetters变量的值已未定义。首先检查是否可以未定义。如果可以,请将行更改为
if(searchItemsSmallLetters && searchItemsSmallLetters.indexOf(searchTextSmallLetters) !== -1){
if(searchItemsSmallLetters && searchItemsSmallLetters.indexOf(searchTextSmallLetters) !== -1){
或者检查“ajax/getCountries.php”上的服务是否正常工作并返回预期的输出。由于您使用的是get请求,因此可以使用浏览器来执行此操作(%domain%/ajax/getCountries.php例如:-localhost:8080/ajax/getCountries.php)
重要提示:进行验证总是很好的。因此,将行更改为
if(searchItemsSmallLetters && searchItemsSmallLetters.indexOf(searchTextSmallLetters) !== -1){
if(searchItemsSmallLetters && searchItemsSmallLetters.indexOf(searchTextSmallLetters) !== -1){
您的searchItemsSmallLetters
未定义。请检查值是否正确。能否使用http.get中的console.log($rootScope.searchItems)和for循环?在某些情况下,$rootScope.searchItems未定义,如果(searchItemsSmallLetters)工作正常。否则你需要检查你的服务。我发现了错误,是在我的服务中。谢谢你的帮助