Html 基于angularjs表达式更改img源
所以我试图根据一个表达式来改变图像的来源,看看它是否为真 HTMLHtml 基于angularjs表达式更改img源,html,css,angularjs,image,Html,Css,Angularjs,Image,所以我试图根据一个表达式来改变图像的来源,看看它是否为真 HTML <img ng-src="category.content.Value >= category.content.ValueOneWeekAgo ? 'Images/green_arrow.png' : 'Images/red_arrow.png'" /> =category.content.ValueOneWeekAgo? 'Images/green_arrow.png':'Images/red_arrow
<img ng-src="category.content.Value >= category.content.ValueOneWeekAgo ?
'Images/green_arrow.png' : 'Images/red_arrow.png'" />
=category.content.ValueOneWeekAgo?
'Images/green_arrow.png':'Images/red_arrow.png'/>
呈现的html是这样的:
<img ng-src="category.content.Value >= category.content.ValueOneWeekAgo ? 'Images/green_arrow.png' : 'Images/red_arrow.png'"
src="category.content.Value >= category.content.ValueOneWeekAgo ? 'Images/green_arrow.png' : 'Images/red_arrow.png'">
=category.content.ValueOneWeekAgo?'Images/green_arrow.png':'Images/red_arrow.png'
src=“category.content.Value=category.content.ValueOneWeekAgo?'Images/green\u arrow.png':'Images/red\u arrow.png'”>
我知道变量包含调试确认的值。可能是一些简单的问题,但我似乎无法解决。您可以将表达式中的顺序切换为使用
=
。
您不能使用=
,因为浏览器将其视为html标记的结尾
<img ng-src="category.content.ValueOneWeekAgo < category.content.Value ? 'Images/green_arrow.png' : 'Images/red_arrow.png'" >
另一种选择是将此代码放入控制器中,并作为新变量发布在
$scope
上,您还想我解释什么?是否要更改控制器中的ng src值?结果与我上面编写的呈现html完全相同,部分原因在于我最终的操作顺序。ng src=“{getArrowSrc(category.content.Value,category.content.ValueOneWeekAgo)}”调用函数不需要{{}
。我认为模板中可能存在其他标记错误,导致ngsrc
属性出错。