AngularJs 过滤少数值不起作用
AngularJs filter for few values doesn't work
我正在使用 angularjs 过滤器,一切正常,但当我搜索薪水为 30 的人时(仅举个例子),它不起作用,如果我再添加一个值,它就会开始工作有人可以解释我有什么问题吗?
笨蛋 link https://plnkr.co/edit/bCz8jq8KSd7XCk2WmHo1?p=info
<!DOCTYPE html>
<html lang="en" ng-app="myModule">
<head>
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body ng-controller="myController">
Rows to display : <input type="number" step="1" ng-model="rowLimit" max="5" min="0" />
Sort By :
<select ng-model="sortColumn">
<option value="name">Name ASC</option>
<option value="+dateOfBirth">Date of Birth ASC</option>
<option value="+gender">Gender ASC</option>
<option value="-salary">Salary DESC</option>
</select>
Search : <input type="text" placeholder="Search employees" ng-model="searchText" />
<!-- Search : <input type="text" placeholder="Search employees" ng-model="searchText.city"/> -->
Hide Salary: <input type="checkbox" ng-model="hideSalary" />
<table>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Date Of Birth</th>
<th>Gender</th>
<th>Salary(number)</th>
<th ng-hide="hideSalary">Salary(currency)</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="employee in emp|limitTo:rowLimit|orderBy:sortColumn | filter : searchText">
<!-- <tr ng-repeat="employee in emp|limitTo:rowLimit|orderBy:'-firstName'"> -->
<td>{{employee.firstName | uppercase}}</td>
<td>{{employee.lastName}}</td>
<td>{{employee.DOB | date:"dd-MM-yyyy"}}</td>
<td>{{employee.gender |lowercase}}</td>
<td>{{employee.salary|number:2}}</td>
<td>{{employee.salary|currency:'$'}}</td>
</tr>
</tbody>
</table>
</body>
</html>
**script.js**
var myApp = angular.module('myModule', []).controller("myController", function ($scope) {
var emp = [
{firstName:"Ben",lastName:"Ding",DOB:new Date('December 26, 1991'),gender:"Male",salary:500},
{firstName:"John",lastName:"Deo",DOB:new Date('December 26, 1991'),gender:"Male",salary:6500.21},
{firstName:"Rajesh",lastName:"kumar",DOB:new Date('December 26, 1991'),gender:"Male",salary:3500},
{firstName:"Rashmi",lastName:"singh",DOB:new Date('December 26, 1991'),gender:"female",salary:8500.78},
{firstName:"soumya",lastName:"kumari",DOB:new Date('December 26, 1991'),gender:"female",salary:30}
]
$scope.emp = emp;
$scope.rowLimit = 5;
$scope.sortColumn = name;
});
未应用过滤器的 DOB 值为“1991-12-25T18:30:00.000Z”,其中包括 30。这就是未应用过滤器的原因。您可以将薪水 30 更改为任何其他数字并尝试,它会起作用。日期值将根据当前日期和时间发生变化。
您可以使用 https://momentjs.com/ 格式化日期值。
我正在使用 angularjs 过滤器,一切正常,但当我搜索薪水为 30 的人时(仅举个例子),它不起作用,如果我再添加一个值,它就会开始工作有人可以解释我有什么问题吗?
笨蛋 link https://plnkr.co/edit/bCz8jq8KSd7XCk2WmHo1?p=info
<!DOCTYPE html>
<html lang="en" ng-app="myModule">
<head>
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body ng-controller="myController">
Rows to display : <input type="number" step="1" ng-model="rowLimit" max="5" min="0" />
Sort By :
<select ng-model="sortColumn">
<option value="name">Name ASC</option>
<option value="+dateOfBirth">Date of Birth ASC</option>
<option value="+gender">Gender ASC</option>
<option value="-salary">Salary DESC</option>
</select>
Search : <input type="text" placeholder="Search employees" ng-model="searchText" />
<!-- Search : <input type="text" placeholder="Search employees" ng-model="searchText.city"/> -->
Hide Salary: <input type="checkbox" ng-model="hideSalary" />
<table>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Date Of Birth</th>
<th>Gender</th>
<th>Salary(number)</th>
<th ng-hide="hideSalary">Salary(currency)</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="employee in emp|limitTo:rowLimit|orderBy:sortColumn | filter : searchText">
<!-- <tr ng-repeat="employee in emp|limitTo:rowLimit|orderBy:'-firstName'"> -->
<td>{{employee.firstName | uppercase}}</td>
<td>{{employee.lastName}}</td>
<td>{{employee.DOB | date:"dd-MM-yyyy"}}</td>
<td>{{employee.gender |lowercase}}</td>
<td>{{employee.salary|number:2}}</td>
<td>{{employee.salary|currency:'$'}}</td>
</tr>
</tbody>
</table>
</body>
</html>
**script.js**
var myApp = angular.module('myModule', []).controller("myController", function ($scope) {
var emp = [
{firstName:"Ben",lastName:"Ding",DOB:new Date('December 26, 1991'),gender:"Male",salary:500},
{firstName:"John",lastName:"Deo",DOB:new Date('December 26, 1991'),gender:"Male",salary:6500.21},
{firstName:"Rajesh",lastName:"kumar",DOB:new Date('December 26, 1991'),gender:"Male",salary:3500},
{firstName:"Rashmi",lastName:"singh",DOB:new Date('December 26, 1991'),gender:"female",salary:8500.78},
{firstName:"soumya",lastName:"kumari",DOB:new Date('December 26, 1991'),gender:"female",salary:30}
]
$scope.emp = emp;
$scope.rowLimit = 5;
$scope.sortColumn = name;
});
未应用过滤器的 DOB 值为“1991-12-25T18:30:00.000Z”,其中包括 30。这就是未应用过滤器的原因。您可以将薪水 30 更改为任何其他数字并尝试,它会起作用。日期值将根据当前日期和时间发生变化。
您可以使用 https://momentjs.com/ 格式化日期值。