Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML code inside an angularjs expression

Tags:

angularjs

Is it possible to render html codes inside an angularjs expression? Something like this code:

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    </head>
    <body ng-app="myApp">
        {{
            1+2 === 3 ? 
            '<span>Hello World!</span>' 
                : 
            "HI" 
        }}
    </body>
    <script>
    var app = angular.module('myApp', []); 
    </script>
</html>
like image 744
Dean Christian Armada Avatar asked Mar 17 '26 16:03

Dean Christian Armada


1 Answers

I think there's definitely a better solution to this using ng-if. Having these huge angular expressions with HTML inside defeats the purpose in my opinion. What I'd do is something like this:

<body ng-app="myApp">
    <span ng-if="1 + 2 === 3">Hello World!</span>
    <p ng-if="1 + 2 !== 3">HI</p>
</body>

This a lot more readable to me.

like image 82
Luka Jacobowitz Avatar answered Mar 21 '26 02:03

Luka Jacobowitz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!