AngularJS:指令 - 传递字符串而不必使用引号

AngularJS: Directive - Passing strings without having to use quotes

本文关键字:不必 字符串 指令 AngularJS      更新时间:2023-09-26

这是我创建的指令:

.HTML:

<p-test something="'bla'"></p-test>

JavaScript:

.directive('pTest', function() {
    return {
        scope: {
            something: '=?'
        },
        templateUrl: 'components/testTemplate.html',
        controller: 'testController'
    };
});

我希望能够通过以下方式将"bla"作为不带"的字符串传递:

<p-test something="bla"></p-test>

我知道可以通过链接中的属性参数实现,但在这种情况下无关紧要(如果我错了,请纠正我),因为我将这些参数直接传递给范围。

我希望能够通过以下方式将"bla"作为不带"的字符串传递:

您只需要文本绑定(@)绑定,而不是2路绑定。

.directive('pTest', function() {
    return {
        scope: {
            something: '@?' //<-- Here
        },
        templateUrl: 'components/testTemplate.html',
        controller: 'testController'
    };
});

如果要绑定范围属性,请使用文本绑定,请使用插值。 即例如,如果 bla 是一个包含字符串的范围变量,那么只需执行以下操作:

 <p-test something="{{bla}}"></p-test>

普罗提