TypeError:无法读取属性' indexOf'在$ scope.search中未定义的

时间:2016-09-23 07:14:52

标签: angularjs

伙计们我是Angular JS的新手。我试图在角度js上进行非常小的POC搜索。

我有一个名为searchText的范围变量但未定义,但我不知道这里有什么问题。

HTML

<!DOCTYPE html>
<html ng-app="myEmp">
<head>
   <title>AJS Event Handler</title>
   <script src="../AngularJS/angular.js"></script>
   <script src="search.js"></script>
   <style type="text/css">

    table {
        border-collapse: collapse;
        font-family: Arial;
    }

    table, td {
        border: 1px solid black;
        padding: 5px;
        text-align: left;
    }

    th{
        border: 1px solid black;
        padding: 5px;
        text-align: left;
        cursor: pointer;
    }

 </style>
 </head>
 <body ng-controller="myController">
<input type="text" ng-model="searchText" placeholder="search by name and   city">
<br/>
<br/>
<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>DOB</th>
            <th>Gender</th>
            <th>Salary</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="emp in employees | filter:search">
            <td>{{ emp.fName }}</td>
            <td>{{ emp.dob | date:"dd/MM/yyyy" }}</td>
            <td>{{ emp.gen }}</td>
            <td>{{ emp.salary | number:2 }}</td>
        </tr>
    </tbody>
</table>

 </body>
 </html>

JS

// create the module, create controller and register controller to module in one line
angular .module("myEmp", [])
        .controller("myController", function($scope){
            var employees = [
                {fName: 'Soham', dob: new Date("may 23,1990"), gen: 'Male', salary: 10000.506666},
                {fName: 'Shardha', dob: new Date("may 23,1995"), gen: 'Female', salary: 10555000.50},
                {fName: 'Bhagya', dob: new Date("april 23,1999"), gen: 'Male', salary: 4343.50},
                {fName: 'Soham', dob: new Date("may 23,1999"), gen: 'Female', salary: 10043434500.50}
            ];

            $scope.employees = employees;
            $scope.searchText = undefined;

            $scope.search = function(item){
                if($scope.searchText == undefined){
                    return true;
                }else{
                    console.log($scope.searchText);
                    if($scope.searchText && item.fName.indexOf($scope.searchText) != -1 || 
                        $scope.searchText && item.city.indexOf($scope.searchText) != -1 ){
                        return true;
                    }
                }
                return false;
            }
        });

1 个答案:

答案 0 :(得分:1)

员工中没有城市领域。只需进行现场检查,然后执行indexOf

angular .module("myEmp", [])
    .controller("myController", function($scope){
        var employees = [
            {fName: 'Soham', dob: new Date("may 23,1990"), gen: 'Male', salary: 10000.506666},
            {fName: 'Shardha', dob: new Date("may 23,1995"), gen: 'Female', salary: 10555000.50},
            {fName: 'Bhagya', dob: new Date("april 23,1999"), gen: 'Male', salary: 4343.50},
            {fName: 'Soham', dob: new Date("may 23,1999"), gen: 'Female', salary: 10043434500.50}
        ];

        $scope.employees = employees;
        $scope.searchText = undefined;

        $scope.search = function(item){
            if($scope.searchText == undefined){
                return true;
            }else{
                console.log($scope.searchText);
                if($scope.searchText && (item.fName.indexOf($scope.searchText) != -1 || item.city && item.city.indexOf($scope.searchText) != -1 )){
                    return true;
                }
            }
            return false;
        }
    });