我想在我的搜索框中替換任何字符作爲★字符,因爲我的數據數組中存儲了所有我的星形數據。AngularJS替換過濾器的字符
我在下面添加了我的代碼。
我是Angular的新手。我還沒有做到這一點。
var app = angular.module("myApp", []);
angular.module('myApp')
.directive('hide-characters', HideCharactersDirective);
function HideCharactersDirective() {
return {
restrict: 'A',
require: ['ngModel'],
link: function(scope, element, attrs, ctrls) {
var ngModelController = ctrls[0];
ngModelController.$formatters.push(function(viewValue) {
return viewValue.replace(/./g, '*');
});
}
};
}
app.controller("myCtrl", function($scope) {
$scope.records = [{
"Name": "Alfreds Futterkiste",
"Star": "★★★"
},
{
"Name": "Berglunds snabbköp",
"Star": "★"
},
{
"Name": "Centro comercial Moctezuma",
"Star": "★★★★"
},
{
"Name": "Ernst Handel",
"Star": "★★"
}
]
});
<html lang="">
<head>
<base target="_blank">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>
<body ng-app="myApp">
<table ng-controller="myCtrl" border="1">
<input hide-characters ng-model="search.Star" type="text">
<tr ng-repeat="x in records | filter:search">
<td>{{x.Name}}</td>
<td>{{x.Star}}</td>
</tr>
</table>
</body>
</html>
所以如果我輸入'test'(4個字符)在搜索框中,你想讓它變得改爲''****( 4星),對吧? – tanmay