[英]I want to show different Divs with data if the string starts with E and A and then delete first two letters from data
I have data coming from a API in Angular. 我有来自Angular API的数据。
$scope.alert = alertMessage;
If the Alert message starts with E. i want to show div 1 如果警报消息以E开头,我想显示div 1
<div class= "alert 1">
<p>{{alert}}</p>
</div>
IF the Alert message starts with A i want to show div 2 如果警报消息以A开头,我想显示div 2
<div class= "alert 2">
<p>{{alert}}</p>
</div>
Also i want to trim first two characters in Alert message. 我也想修剪警报消息中的前两个字符。
Can anybody help me on this. 有人可以帮我吗?
//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 toggles the appearance of the element by adding the CSS display: none style. ng-show通过添加CSS显示:无样式来切换元素的外观。 ng-if, on the other hand, actually removes the element from the DOM when the condition is false and only adds the element back once the condition turns true.
另一方面,ng-if实际上在条件为false时从DOM中删除该元素,并且仅在条件变为true时才将元素添加回去。 Just depends one what your requirements are but you could essentially use either one.
仅取决于您的需求是什么,但实际上您可以使用其中之一。
$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>
I have used ng-if for two divs like what taplar told. 我已经将ng-if用于两个div,就像taplar所说的那样。 in case of removing 2 characters i used {{alert.substring(2)}}
在删除2个字符的情况下,我使用了{{alert.substring(2)}}
This worked . 这行得通。 Thanks Guys for your help.
谢谢大家帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.