使用AngularJS和php TypeError的自动完成搜索无法读取属性';搜索文本';未定义的

使用AngularJS和php TypeError的自动完成搜索无法读取属性';搜索文本';未定义的,php,angularjs,Php,Angularjs,我正在尝试进行自动完成搜索,因此我想从php文件中获取数据,但出现以下错误: TypeError:无法读取未定义的属性“searchText” 我不知道是什么问题,我试了两天 这是我的密码: clientToServer.js function CltToServer($scope, $rootScope, $http) { $http({ method: "get", url: "PureAngualr/SearchResultsQuery.php",

我正在尝试进行自动完成搜索,因此我想从php文件中获取数据,但出现以下错误:

TypeError:无法读取未定义的属性“searchText”

我不知道是什么问题,我试了两天

这是我的密码:

clientToServer.js

function CltToServer($scope, $rootScope, $http) {
    $http({
        method: "get",
        url: "PureAngualr/SearchResultsQuery.php",
        dataType: 'json',
        data: {searchText: $rootScope.searchText},
        async: false,

        params: {
            action: "get"
        }
    }).success(function (data, status, headers, config) {
        $rootScope.suggestions = response.data;
        alert(2);

    }).error(function (data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
    });
}
app1.controller('autoCompleteCTRL', function ($scope, $rootScope, $http) {

    //Sort Array
    /*$rootScope.searchItems.sort();*/
    //Define Suggestions List
    $scope.searchText = '';
    $scope.selectedItem = undefined;
    $rootScope.suggestions = undefined;
    //Define Selected Suggestion Item
    $rootScope.selectedIndex = -1;


    //Function To Call On ng-change
    $rootScope.search = function ($scope, $rootScope, $http) {

        $rootScope.searchItems = CltToServer($scope, $rootScope, $http);

        alert(1);
        $rootScope.suggestions = CltToServer($scope, $rootScope, $http);
        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;
                }
            }
        }
    };
autocomplete.js

function CltToServer($scope, $rootScope, $http) {
    $http({
        method: "get",
        url: "PureAngualr/SearchResultsQuery.php",
        dataType: 'json',
        data: {searchText: $rootScope.searchText},
        async: false,

        params: {
            action: "get"
        }
    }).success(function (data, status, headers, config) {
        $rootScope.suggestions = response.data;
        alert(2);

    }).error(function (data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
    });
}
app1.controller('autoCompleteCTRL', function ($scope, $rootScope, $http) {

    //Sort Array
    /*$rootScope.searchItems.sort();*/
    //Define Suggestions List
    $scope.searchText = '';
    $scope.selectedItem = undefined;
    $rootScope.suggestions = undefined;
    //Define Selected Suggestion Item
    $rootScope.selectedIndex = -1;


    //Function To Call On ng-change
    $rootScope.search = function ($scope, $rootScope, $http) {

        $rootScope.searchItems = CltToServer($scope, $rootScope, $http);

        alert(1);
        $rootScope.suggestions = CltToServer($scope, $rootScope, $http);
        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;
                }
            }
        }
    };
app1.controller('autocomplettectrl',function($scope、$rootScope、$http){
//排序数组
/*$rootScope.searchItems.sort()*/
//定义建议列表
$scope.searchText='';
$scope.selectedItem=未定义;
$rootScope.suggestions=未定义;
//定义所选建议项
$rootScope.selectedIndex=-1;
//调用ng change的函数
$rootScope.search=函数($scope、$rootScope、$http){
$rootScope.searchItems=CltToServer($scope、$rootScope、$http);
警报(1);
$rootScope.suggestions=CltToServer($scope、$rootScope、$http);
var myMaxSuggestionListLength=0;
对于(变量i=0;i<$rootScope.searchItems.length;i++){
var searchItemsSmallLetters=angular.lowercase($rootScope.searchItems[i]);
var searchtextsalllets=angular.lowercase($scope.searchText);
if(searchItemsSmallLetters.indexOf(searchTextSmallLetters)!=-1){
$rootScope.suggestions.push(searchItemsSmallLetters);
myMaxSuggestionListLength+=1;
如果(myMaxSuggestionListLength==5){
打破
}
}
}
};
index.html

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script>
<div ng-controller="autoCompleteCTRL">
    <input type="text" placeholder="Search for items" id="textFiled" class="input" ng-keydown="checkKeyDown($event)" ng-keyup="checkKeyUp($event)" ng-model="searchText" ng-change="search()"/>
    <ul class="suggestions-list ">
        <li ng-repeat="suggestion in suggestions track by $index" ng-class="{active : selectedIndex === $index}" ng-click="AssignValueAndHide($index)">
            {{suggestion}}
        </li>
    </ul>
</div> 

  • {{建议}

提前感谢:)

您的searchText是在$scope或$rootScope中定义的?可能是错误的,但
url:“PureAngualr/SearchResultsQuery.php”
在我看来像是一个打字错误。它不是
PureAngular
?PureAngular只是我放置php文件的一个字段。最后我发现问题,我必须向clientToServer.js添加控制器,因为我使用了两个单独的js文件。但是没有发生任何事情,建议列表没有出现!!你知道为什么吗?我的函数在au中有一些问题tocomplete.js?您的搜索文本是在$scope或$rootScope中定义的?可能是错误的,但
url:“PureAngualr/SearchResultsQuery.php”
在我看来像是一个打字错误。它不是
PureAngular
?PureAngular只是我放置php文件的一个字段。最后我发现问题,我必须向clientToServer.js添加控制器,因为我使用了两个单独的js文件。但是没有发生任何事情,建议列表没有出现!!你知道为什么吗?我的函数在au中有一些问题tocomplete.js?