Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular, output from function on expression

I want to know if it is possible to do the following:

<div ng-repeat='article in articles | filter:search'>
...
    <div>
        {{marked(article.body)}}
    </div>
...
</div>

So I want to execute the "marked" function, passing the article body as a parameter and display the generated output.

like image 509
Arne Schreuder Avatar asked Sep 08 '14 08:09

Arne Schreuder


People also ask

What is the output of this AngularJS expression?

Expressions are used to bind application data to HTML. Expressions are written inside double curly braces such as in {{ expression}}. Expressions behave similar to ngbind directives. AngularJS expressions are pure JavaScript expressions and output the data where they are used.

What does :: mean in Angular?

It means it's used to achieve one time binding. Example. angular.

How do you call a ngIf function?

Try *ngIf="condition && yourfunction()" . Your function must return true to the if evaluate to true, but it will only be executed if your condition is true, since an and operator will stop on first false.

What is $eval in AngularJS?

$eval evaluates an Angular expression against/on the current scope.


2 Answers

Sure, no problem with that syntax ! :)

All you need is to make sur the marked function is defined in the right scope. For instance, let's assume you are in the ArticleCtrl controller :

app.controller('ArticleCtrl', function($scope) {

    // Declare the method in the controller's scope
    $scope.marked = function(article_body) {

         // do whatever you want here
         // and don't forget to return the expected result
         return "LOVE CAPS! " + article_body.toUpperCase();
    };
 });

You can then use {{ marked(something) }} in you template.

like image 117
giant_teapot Avatar answered Oct 10 '22 01:10

giant_teapot


it's possible, but make sure that function will be $scope function.

Of course call function in ng-repeat isn't good idea, try to re think about your architecure and maybe create some model for it.

like image 25
bmazurek Avatar answered Oct 10 '22 01:10

bmazurek