Call subroutines directly from ng-if and ng-bind-html - Forum

Forum Navigation
You need to log in to create posts and topics.

Call subroutines directly from ng-if and ng-bind-html

I have just discovered this functionality :)
You can use ng-if advanced property to show or hide an object. For example if you add this to ng-if property:

[myvar]=="myvalue"

Then, if [myvar] contains anything different from "myvalue", the object will remain invisible.
Additionaly you can create a subroutine and send a parameter to it. If the subroutine returns true, the object will be shown.
I mean you can add this code directly into ng-if property too:

mysubroutine([myvar]);

For ng-bind-html is exactly the same, but the subroutine can return any HTML content. For example imagine you send [data] to the subroutine, just like this:

mysubroutine([mydata])

And then the subroutine gets the parameter as [data] and returns an HTML string just like this:

return "Hello <b>[data]</b>"

Then the object content will show:

Hello Value.

This is very useful to format content from a database or JSON using ng-repeat.

I hope you find it as useful as myself.

Vadim, Ivanes82 and 3 other users have reacted to this post.
VadimIvanes82CDY@44smartmediaroccocogliano

Because ng-bind-html seems to have also the ngsanitize module available you can also use the linky filter.

So for example this is possible in ng-bind-html

tossu | linky:'_blank'

and if you variable is

$App.tossu = 'Text with links: https://docs.angularjs.org/api/ngSanitize/filter/linky';

The result is then html where anchor tags are added automatically.

 

luishp has reacted to this post.
luishp