如果字符串以E和A开头,然后从数据中删除前两个字母,我想用数据显示不同的Div



我的数据来自Angular中的API。

$scope.alert = alertMessage;

如果Alert消息以E.开头,我想显示div 1

<div class= "alert 1">
<p>{{alert}}</p>
</div>

如果Alert消息以A开头,我想显示div 2

<div class= "alert 2">
<p>{{alert}}</p>
</div>

此外,我想修剪警报消息中的前两个字符。

有人能帮我吗。

//If the Alert message starts with E. i want to show div 1
<div class="alert 1" ng-if="alert[0] === 'E'"><!-- show if the first character is E -->
<p>{{alert.slice(2)}}</p><!-- remove the first two characters -->
</div>

ng-show通过添加CSS display:none样式来切换元素的外观。另一方面,如果在条件为false时实际上从DOM中删除了元素,并且只有在条件为true时才将元素添加回来。这取决于你的需求,但你基本上可以使用任何一种。

$scope.alert = alertMessage.slice(2);
<div class= "alert 1" ng-show="alert.indexOf('E') == 0"><p>{{alert}}</p>
</div>   
<div class= "alert 2" ng-show="alert.indexOf('A') == 0">
<p>{{alert}}</p>
</div>

我对两个div使用了ng if,就像taplar告诉的那样。在删除2个字符的情况下,我使用了{{alert.substring(2(}}

这起到了作用。谢谢伙计们的帮助。

最新更新