javascript - ExtJs:如何在 html 中使用变量

标签 javascript html variables extjs web-applications

我正在尝试创建一个使用选项卡的 ExtJS 应用程序。我希望能够为每个选项卡的相应容器设置 div id。

到目前为止我有这个:

Ext.define('EXT.view.tab.tabs', {
    extend: 'Ext.container.Container',
    xtype: 'tabs',
    controller: 'tabcontroller',

    layout: 'fit',
    config: {
        name: 'image'
    },
    items: [{
        xtype: 'container',
        html: '<div id="{name}"'
    }]
});

所以显然这是行不通的。如何在 HTML 中使用变量?

还有其他方法可以做到这一点吗?我显然在这里遗漏了一些东西。

感谢您的帮助。

最佳答案

由于您在config中设置名称,因此无法直接访问。为此,您需要使用 initComponent()方法。在方法内部,您可以像这样访问您的配置

this.getName()

在此Fiddle ,我使用 initComponent()viewModel 创建了一个演示。

代码片段:

//Using initComponent method set the id dynamically
Ext.define('TabsView', {
    extend: 'Ext.container.Container',
    xtype: 'tabs',
    //controller: 'tabcontroller',

    layout: 'fit',

    config: {
        name: 'image',
        text: `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.`
    },

    initComponent: function() {
        this.callParent();
        this.add([{
            xtype: 'container',
            html: `<div id=${this.getName()}>${this.getText()}</div>`
        }])
    }
});

Ext.create({
    xtype: 'tabs',
    renderTo: Ext.getBody()
});

您还可以通过 viewModel 实现相同的功能

代码片段:

//Using viewModel
Ext.define('TabsView1', {
    extend: 'Ext.panel.Panel',
    xtype: 'tabs1',
    title: 'Using Viewmodel',
    layout: 'fit',
    margin: '20 0',
    bodyPadding: 10,

    viewModel: {
        data: {
            name: 'image',
            text: `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.`
        }
    },

    items: [{
        xtype: 'container',
        bind: '<div id={name}>{text}</div>'
    }]
});

Ext.create({
    xtype: 'tabs1',
    renderTo: Ext.getBody()
});

关于javascript - ExtJs:如何在 html 中使用变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50900960/

相关文章:

javascript - JQuery 菜单触发内容

Jquery UI 位置插件不适用于 margin 属性

html - 响应式电子邮件表不断中断 - 是什么原因造成的?

Python:根据条目的位置删除列表中的单个条目

powershell - 使用 if 语句检查变量是否为空返回错误

javascript - 如何使用响应式菜单防止正文滚动

javascript - Dart-JS 互操作 : JS object not loading when running in Dartium

javascript - 如何将 javascript 变量的值替换为表单字段?

css - SCSS/SASS 中带有 Angular 分量的全局变量

javascript - 切换 v-navigation-drawer 的悬停扩展不调整内容大小