Javascript 如何写日期“日期:”;“MM”;在ng类中?
当Javascript 如何写日期“日期:”;“MM”;在ng类中?,javascript,angularjs,date,Javascript,Angularjs,Date,当agent.createdAt | date:“MM”等于todaymount时,我想添加类month(代码在指令中): “{todaymount}{agent.createdAt|date:\“yyyy-MM-dd HH:MM\”}”+ 我不知道如何添加agent.createdAt |日期:“MM”。如果我这样做: {'month\':agent.createdAt日期:\'MM\'==todaymonth}引发与{'month\'相同的意外表达式错误:(agent.createdAt日
agent.createdAt | date:“MM”
等于todaymount
时,我想添加类month
(代码在指令中):
“{todaymount}{agent.createdAt|date:\“yyyy-MM-dd HH:MM\”}”+
我不知道如何添加agent.createdAt |日期:“MM”
。如果我这样做:
{'month\':agent.createdAt日期:\'MM\'==todaymonth}
引发与{'month\'相同的意外表达式错误:(agent.createdAt日期:\'MM\”==todaymonth}
如何正确执行此操作?我使用了Date类的getMonth()方法,该方法的开始索引为0,因此我在表达式中添加了1
'<td ng-class="{ month: agent.createdAt.getMonth()+1==todaymonth}"><span>{{todaymonth}}</span>{{agent.createdAt | date:\"yyyy-MM-dd HH:mm\"}}</td>'+
“{todaymount}{agent.createdAt|date:\“yyyy-MM-dd HH:MM\”}”+
既然要输入表达式,只需使用class
而不是ng class
:
<td class="{{(createdAt | date:'MMM') == todayMonth ? (createdAt | date:'MMM') : ''}}">...</td>
。。。
上面的代码将根据月份为您的
提供1月、2月等类别
我对类名是数字有一些问题,所以我改用MMM
todayMonth
需要类似于“Jan”
这显示了整个事件的实际情况
注意:为了更好的可读性,我更改了一些属性名称,所以仅仅复制和粘贴到代码中是行不通的。出于某种原因,如果我执行
agent.createdAt.getMonth()
可能getMonth()
在角度模板中是禁止的?您的createdAt是日期对象吗?是的,它是JavaScript的日期对象<代码>{“objectId”:“564aa46260b294bc1277cc25”,“用户名”:“+886911111111”,“realname”:“John Ian”,“emailVerified”:false,“createdAt”:“2015-11-17T03:52:02.707Z”,“reports”:哦,不,我想是字符串。
<td class="{{(createdAt | date:'MMM') == todayMonth ? (createdAt | date:'MMM') : ''}}">...</td>