Angular,表达式函数的输出

Arn*_*der 11 angularjs angularjs-ng-repeat

我想知道是否可以执行以下操作:

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

所以我想执行"标记"功能,将文章正文作为参数传递并显示生成的输出.

gia*_*pot 11

当然,语法没问题!:)

您所需要的只是使marked功能在正确的范围内定义.例如,假设您在ArticleCtrl控制器中:

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();
    };
 });
Run Code Online (Sandbox Code Playgroud)

然后{{ marked(something) }},您可以在模板中使用.

  • 只是一个建议,对于这种类型或数据转换,建议构建一个过滤器,它使用相同的逻辑,并且可以在整个应用程序中重用。 (2认同)