0
我似乎無法得到此工作。我試圖用字母「T」替換任何空的空間來將我的字符串轉換爲日期格式,但我似乎無法得到它的工作。我的數組在我使用console.log時可以工作,並且可以看到它是yyyy-MM-ddTHH:mm格式。但是,當我把它與ng-repeat過濾器連接時,它什麼也不做。這裏是我的代碼,並鏈接到JSbin https://jsbin.com/nesedenoce/edit?html,js,console,output角度濾鏡替換3d陣列中的字符串
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
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;
}
});
不清楚爲什麼你需要在這個陣列上的過濾器。 'start_time'嵌套在'persons'對象中。只是客觀的解析日期? – charlietfl
請注意,這可能會更容易使'persons'成爲'揚聲器'的方式 – charlietfl
我把'$ scope.returnedObj'作爲一個數組放在這裏,但在我的原始代碼中,我放入了數千個看起來像這樣的記錄來自json_encoded的PHP。但格式看起來非常類似於我的原始json字符串。 – missgg