我想通过字符串的最后一个字符使用AngularJS搜索字符串。我用第一封信完成了搜索。我试过lastIndexOf()
和endswith,但我无法得到结果。如何在angularjs中查找带有最后一个字母的字符串?
这一个工作正常,它的第一个字母搜索字母。
var app = angular.module('filterSample', []);
app.controller('filterCtrl', function($scope) {
$scope.data = {
messages: ['first', 'second', '3rd', 'fourth', 'fifth']
}
$scope.startsWith = function(actual, expected) {
var lowerStr = (actual + "").toLowerCase();
return lowerStr.indexOf(expected.toLowerCase()) === 0;
}
});
<!doctype html>
<html ng-app="filterSample">
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js">
</script>
<script src="example.js"></script>
</head>
<body>
<div ng-controller="filterCtrl">
<input type="text" ng-model="search" />
<ul border="1px" ng-repeat="msg in data.messages | filter:search:startsWith">
<li>{{msg}}</li>
</ul>
</div>
</body>
</html>
可使用内建的功能[String.prototype.endsWith()](https://developer.mozilla.org/ en/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith)和[String.prototype.startsWith()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith) – Satpal
可能的重复[How我可以使用JavaScript获取字符串的最后一个字符](https://stackoverflow.com/questions/5873810/how-can-i-get-last-characters-of-a-string-using-javascript) –
谢谢Satpal和Tim Biegeleisen –