Javascript AngularJS:在ng repeat中修改值
我有一个简单的角度表:Javascript AngularJS:在ng repeat中修改值,javascript,angularjs,Javascript,Angularjs,我有一个简单的角度表: <table> <tr ng-repeat="row in $data"> <td>{{row.name}}</td> <td>{{row.surname}}</td> </tr> </table> {{row.name} {{row.姓氏}} 这将导致如下情况: <table> <tr>
<table>
<tr ng-repeat="row in $data">
<td>{{row.name}}</td>
<td>{{row.surname}}</td>
</tr>
</table>
{{row.name}
{{row.姓氏}}
这将导致如下情况:
<table>
<tr>
<td>Johnathan</td>
<td>Smith</td>
</tr>
<tr>
<td>Jane</td>
<td>Doe</td>
</tr>
</table>
约翰纳森
史密斯
简
雌鹿
但是我有一个动态搜索函数,它可以重新加载表,我需要在结果中突出显示搜索字符串,就像这样(搜索词是“John”):
约翰纳森
史密斯
现在我希望这样的事情能奏效:
<table>
<tr ng-repeat="row in $data">
<td>{{myFunction(row.name)}}</td>
<td>{{row.surname}}</td>
</tr>
</table>
{{myFunction(row.name)}
{{row.姓氏}}
但事实并非如此。有什么办法可以让这一切顺利吗
更新:已解决,@loan提出的解决方案在本例中有效。使用ng类,因此td的html将变为
<td ng-class = "{red: row.name == searchStr}">{{row.name}}</td>
{{row.name}
ng类还有其他格式,我的html可能不太可靠。我主要使用haml,但请检查上的文档,如下面的示例所示,您可以执行类似的操作 在现有循环中,可以按如下方式添加自定义过滤器:
<body ng-controller="TestController">
<h1>Hello Plunker!</h1>
<input type="text" ng-model="query" />
<ul>
<li ng-repeat="item in data | filter:query">
<!-- use the custom filter to highlight your queried data -->
<span ng-bind-html="item.name | highlight:query"></span>
</li>
</ul>
</body>
你好,普朗克!
-
在JavaScript文件中,您可以创建自定义过滤器:
(function() {
'use strict';
angular.module("app", []);
//to produce trusted html you should inject the $sce service
angular.module("app").filter('highlight', ['$sce', function($sce) {
function escapeRegexp(queryToEscape) {
return queryToEscape.replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1');
}
return function(matchItem, query) {
return $sce.trustAsHtml(query ? ('' + matchItem).replace(new RegExp(escapeRegexp(query), 'gi'), '<strong>$&</strong>') : matchItem);
};
}]);
angular.module("app")
.controller('TestController', ['$scope',
function($scope) {
$scope.query = ""; //your scope variable that holds the query
//the dummy data source
$scope.data = [{
name: "foo"
},{
name: "bar"
},
{
name: "foo bar"
}];
}
]);
})();
(函数(){
"严格使用",;
角度模块(“应用程序”,[]);
//要生成受信任的html,您应该注入$sce服务
角度.module(“app”).filter('highlight',['$sce',function($sce){
函数escapeRegexp(queryToEscape){
返回queryToEscape.replace(/([.?*+^$[\]\\(){}124;-])/g,“\\$1”);
}
返回函数(匹配项、查询){
返回$sce.trustAsHtml(查询?(''+matchItem).替换(新的RegExp(escapeRegexp(查询),'gi'),'$&'):matchItem);
};
}]);
角度模块(“应用程序”)
.controller('TestController',['$scope',
职能($范围){
$scope.query=”“;//保存查询的范围变量
//虚拟数据源
$scope.data=[{
姓名:“富”
},{
名称:“酒吧”
},
{
名称:“富吧”
}];
}
]);
})();
如果需要,可以使用值替换筛选器中的html:
<strong>$&</strong>
$&
到
$&
您可以使用模块。您可以这样简单地使用它:
<table>
<tr>
<td>Johnathan</td>
<td>Smith</td>
</tr>
<tr>
<td>Jane</td>
<td>Doe</td>
</tr>
</table>
HTML
<div>
<input type="text" ng-model="searchText" placeholder="Enter search text" />
</div>
<input type="checkbox" ng-model="caseSensitive" /> Case Sensitive?
<table>
<tbody>
<tr ng-repeat="row in $data">
<td ng-bind-html="row.name | highlight:searchText:caseSensitive"></td>
<td>{{row.surname}}</td>
</tr>
</tbody>
</table>
区分大小写?
{{row.姓氏}}
您可以通过bower下载,其中提供了说明
注意:添加angular的以避免
$sce
不安全错误。您应该使用过滤器。参见示例使用自定义筛选器->和绑定html不安全(我使用类似于
的东西,我不需要突出显示整个值,只需要其中的一个搜索字符串。@Cabellero。然后您可能需要$sce.trustAsHtml(html\U代码)。我回答时正在看您的示例Johnathan。
<div>
<input type="text" ng-model="searchText" placeholder="Enter search text" />
</div>
<input type="checkbox" ng-model="caseSensitive" /> Case Sensitive?
<table>
<tbody>
<tr ng-repeat="row in $data">
<td ng-bind-html="row.name | highlight:searchText:caseSensitive"></td>
<td>{{row.surname}}</td>
</tr>
</tbody>
</table>