简体   繁体   中英

How to use ng-src when there are more conditions to check?

I have 10 icons where each one for different types of alerts. I'm getting the type value from a service. I need to change the icon and style of text based on the type I'm getting from service. I'm using ng-src for changing the images.

    <div ng-src="{{type=='0' : 'img1.png' : (type=='1' : 'img2.png' : ())}}"></div>
    <div ng-class="{{class1: type=='1'}}">Some text</div>

Is there a better way to do this?

You can just set the icon img source in controller. Or even set the img source like:

$scope.iconSrc = "img" + type + ".png";

And use in template. Same login for class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM