我有一个$scope.myData
对象,其中包含我拥有/租用的地址和日期列表,例如:
Example of my data
From Date 2011 To Date current
From Date 2010 To Date 2011
From Date 2009 To Date 2010
From Date 2003 To Date 2004
我要做的是输出一个声明,显示我过去5年拥有/租用的年份。
JS:
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.myData = {
"Entries": [{
"EntryNumber": "E1",
"FromDt": "/Date(1320912000000)/",
"ToDt": null,
"RegisteredWith": "test"
}, {
"EntryNumber": "A1",
"FromDt": "/Date(1276153200000)/",
"ToDt": "/Date(1320912000000)/",
"RegisteredWith": "test"
}, {
"EntryNumber": "X1",
"FromDt": "/Date(1239346800000)/",
"ToDt": "/Date(1276153200000)/",
"RegisteredWith": "test"
}, {
"EntryNumber": "Z1",
"FromDt": "/Date(1063177200000)/",
"ToDt": "/Date(1086850800000)/",
"RegisteredWith": "test"
}
]
}
});
HTML:
<p>
In the last 5 years i have had addresses during (from {{ myData.Entries.FromDt.substring(6, myData.Entries.FromDt.length - 2) | date:'yyyy' }} to {{ }}, {{ }} to {{ }}, {{ }} to {{ }} and {{ }} to {{ }}).
</p>
预期输出示例:
<p>
In the last 5 years i have had addresses during (from 2011 to current, 2010 to 2011 and 2009 to 2010)
</p>
这里是一名掠夺者:http://plnkr.co/edit/N55Zlb2ahjgovoVwTV63?p=preview
答案 0 :(得分:1)
您似乎需要在filter
上使用ng-repeat
和<span>
。您的ng-repeat
<li>
<span>
中的逻辑基本上是正确的。您可以将其移至<p>
In the last 5 years i have had addresses during (<span ng-repeat="data in myData.Entries | filter:onlyLastFiveYears"> -from {{ parseDate(data.FromDt) }} to {{ parseDate(data.ToDt) }}</span>)
</p>
以保留括号之间的所有内容。
的index.html:
app.controller('MainCtrl', function($scope) {
// ...
$scope.parseDate = function(date) {
return ((date === null && 'current') || new Date(parseInt(date.substring(6, date.length - 2))).getFullYear())
}
$scope.onlyLastFiveYears = function(data) {
var currentYear = new Date().getFullYear();
var houseYear = $scope.parseDate(data.FromDt);
return (currentYear - houseYear <= 5);
}
});
app.js:
{{1}}
我已经编辑了你的plunkr here。祝你好运!