[英]How can I apply an Angularjs Directive only if a particular filter is applied?
I am retro-fitting angular into an existing project where the date output is in an undesired format. 我正在将angular改装到现有项目中,该项目的日期输出格式不理想。 (2013/01/01)
(2013/01/01)
I would like to use the AngularJs date filter to format the dates. 我想使用AngularJs日期过滤器来格式化日期。 Unfortunately the current date strings are not is a standard ISO format and I cannot change them.
不幸的是,当前日期字符串不是标准的ISO格式,因此我无法更改它们。
So I figured, the best way would be to apply a new filter before the standard date one, to parse the date. 所以我想,最好的方法是在标准日期之前应用一个新过滤器来解析日期。
ng-bind="prop.ApplicationDate | DateParse | date:fullDate"
angular.module('rjDateParse', [])
.filter('DateParse', function() {
return function(date) {
var parsedDate;
try {
parsedDate = Date.parse(date);
} catch (err){
parsedDate = date;
}
return parsedDate;
}
})
This works fine. 这很好。
Except I am aware of the issues I will have with certain dates in this format. 除了我知道使用这种格式的某些日期会遇到的问题。 2013/12/01 for instance, could come back with two completely different dates.
例如,2013/12/01可能会返回两个完全不同的日期。
So I wanted to create a directive that allowed the date to be clicked to toggle between the original and the parsed date. 因此,我想创建一个指令,该指令允许单击日期以在原始日期和解析的日期之间切换。
My problem: How to apply this directive ONLY if the filter has been used. 我的问题:仅在使用过滤器的情况下,才应用此指令。 I don't want to have to add a class or attribute to the element manually to trigger this.
我不想手动向该元素添加一个类或属性来触发此操作。
angular.module('rjDateParse', [])
.filter('DateParse', function() {
return function(date) {
var parsedDate;
try {
parsedDate = Date.parse(date);
} catch (err){
parsedDate = date;
}
return parsedDate;
}
})
.directive('DateParse', function() {
return {
//apply this only if dateparse filer is applied
}
});
Thanks in advance 提前致谢
Rob 抢
ps I don't have any controllers currently - I am trying to keep it that way :) ps我目前没有任何控制器-我正在尝试保持这种方式:)
You can change format of angular "date" filter in simple way: 您可以通过简单的方式更改角度“日期”过滤器的格式:
{{prop.dateToFormat | date:'yyyy-MM-dd'}}
This code formats date to standard ISO-format 此代码格式日期为标准ISO格式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.