javascript - AngularJS:指令 - 无需使用引号即可传递字符串

标签 javascript angularjs angularjs-directive angularjs-scope

这是我创建的指令:

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>

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

最佳答案

I'd like to be able to pass 'bla' as a string without the '', in the following way:

为此,您只需要文本绑定(bind) (@) 绑定(bind),而不是双向绑定(bind)。

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

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

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

Plnkr

关于javascript - AngularJS:指令 - 无需使用引号即可传递字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27758841/

相关文章:

javascript - 使用 Javascript 将多个文件下载为 zip 或文件夹?

javascript - Angular UI-Router - 在状态更改时刷新延迟加载的嵌套 ui-view

javascript - 我可以强制我的 Angular Directive(指令)将其模板作为同级元素添加到指令元素吗?

angularjs - Angular-Kendo UI Treeview ,节点扩展问题

javascript - 如何使用 javascript 将文本框的值赋予 ngModel 文本框

javascript - 如何使用 angular.js View 刷新页面?

javascript - JavaScript 上的 SOAP 服务调用在 Safari 中有效,但在 Chrome 或 Firefox 中无效

javascript - window.on ('load' , function() {}) 完成后页面元素仍在加载?

javascript - 如何将具有 2 个嵌套对象的数组展平并使其成为一个

Javascript - 如何清除最后绘制的矩形,以便在 'dragging' 创建矩形时仅绘制 1