如何在只接受字符串值/文字的HTML属性中调用函数

How to call function in an HTML attribute that only accepts string values / literals?

本文关键字:HTML 文字 属性 函数 调用 字符串      更新时间:2023-09-26

我在调用这个jade文件中的属性中的函数时遇到问题:

测试。翡翠

script
    - function myFunction(str) {
        some regex on str, say, to filter out all full stops and 
        return the new string.
    }
    .container-fluid
        .row
            .col-md-12
                div(ng-repeat='thing in things')
                    button(popovertext='{{thing.description()}}', popover-trigger='mouseenter')

popovertext属性中的表达式在不调用函数的情况下,为事物中的每一件事打印字符串(如ng repeat所做的)。但据我所知,popovertext只接受字符串值,所以当我做类似的事情时

                    button(popovertext='myFunction({{thing.description()}})')

它实际上打印出了myFunction(….原始字符串….),这基本上意味着它根本无法识别我的函数。我应该在这个jade文件中调用函数,而不是在另一个.js文件中调用外部函数,因为显然任何没有"ng-"的属性都不能与控制器通信?

我怎么可能绕过这个?


此外,我对什么时候应该在代码行前面加一个"-"感到非常困惑。我说的对吗,对于翡翠,我需要它来进行var、function和return?

解决这一问题的一种可能方法是为所有对象设置名为descriptionString的字段。你可以在你的控制器中做到这一点:

angular.forEach(things, function(thing){
  thing.descriptionString = thing.description();
});

剩下的很简单:

button(popovertext='{{thing.descriptionString}}', popover-trigger='mouseenter')