[英]Angular filter to replace a string in a 3d array
I can't seem to get this working. 我似乎无法正常工作。 I am trying to replace any empty space with letter "T" to convert my string to a date format but I can't seem to get it working.
我正在尝试用字母“ T”替换任何空格,以将字符串转换为日期格式,但似乎无法正常工作。 My array works when I console.log and can see that it's yyyy-MM-ddTHH:mm format.
当我console.log时,我的数组可以工作,并且可以看到它是yyyy-MM-ddTHH:mm格式。 But when I put connect it with ng-repeat filter, it doesn't do anything.
但是当我用ng-repeat filter连接它时,它什么也没做。 Here's my code and link to the JSbin https://jsbin.com/nesedenoce/edit?html,js,console,output
这是我的代码,并链接到JSbin https://jsbin.com/nesedenoce/edit?html,js,console,output
HTML HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<title>date filter</title>
</head>
<body>
<div ng-app="test" ng-controller="controller">
<table class="table table-bordered table-striped">
<tr id="theader">
<thead>
<th>Talk Title</th>
<th>Start Time</th>
<th>End Time</th>
</thead>
</tr>
<tr ng-repeat="item in returnedObj | dateFilter ">
<td>{{item.presentation.title}}</td>
<td>{{item.presentation.start_time | date: "h:mm a"}}</td>
<td>{{item.presentation.end_time | date: "h:mm a"}}</td>
</tr>
</table>
</div>
</body>
</html>
JS JS
var app = angular.module('test', []);
app.controller("controller", ['$scope', function($scope){
$scope.returnedObj = [{"presentation":{"title":"Introduction to Algebra",
"persons":{"1":{"id":22,"start_time":"2017-05-19 08:00","end_time":"2017-05-19 08:20"},
"speakers":[{"firstname":"Charles","lastname":"Hardin"}]}}}];
}]);
app.filter('dateFilter', function(){
return function(item){
var correctTimeFilter = item.filter(function(i){
if(i.presentation.start_time || i.presentation.end_time){
console.log(i.presentation.start_time.replace(/\s/g, 'T'));
console.log(i.presentation.end_time.replace(/\s/g, 'T'));
return i.presentation.start_time.replace(/\s/g, 'T');
return i.presentation.end_time.replace(/\s/g, 'T');
}
else
return null;
})
return correctTimeFilter;
}
});
You are reading your object wrong. 您读错了对象。 You are trying to get values from where they are not.
您正在尝试从不是的地方获取价值。
It is much cleaner when you format it like this: 像这样格式化它会更加干净:
$scope.returnedObj = [{
"presentation":{
"title":"Introduction to Algebra",
"persons":{
"1":{
"id":22,"start_time":"2017-05-19 08:00","end_time":"2017-05-19 08:20"
},
"speakers":[{
"firstname":"Charles","lastname":"Hardin"
}]
}
}
}];
Here is the snippet that does replace empty space in dates with T
if that is what you wanted to do. 如果您要执行的操作,则以下代码片段将
T
空白区域替换为T
Also you can't return 2 values in sequence as you tried in filter function. 同样,您不能像在过滤器函数中尝试的那样顺序返回2个值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.