简体   繁体   English

将Angular html模板传递给主干模板

[英]Pass Angular html template to backbone template

I'm trying to slowly convert a Backbone App to Angular. 我正在尝试将Backbone应用程序缓慢转换为Angular。 It's actually using Marionette to be precise. 确切地说,它实际上是在使用木偶。 In each of my current Marionette views, they are a Marionette.ItemView. 在我当前的每个“木偶”视图中,它们是一个Marionette.ItemView。 For the template to show, I do this: 为了显示模板,我这样做:

    get template() {
        return _.template(nameOfMyTemplate, { myModel: this.model });
    }

nameOfMyTemplate would be my AwesomeTemplate.template.html file that is in my solution. nameOfMyTemplate将是我的解决方案中的AwesomeTemplate.template.html文件。

I was wondering how I could go about just passing my template.html without calling _.template in a backbone or marionette app? 我想知道如何在不通过骨干网或木偶应用程序中调用_.template的情况下传递我的template.html? So somewhere in my backbone view or marionette.itemview, I could do 因此,在我的骨干视图或marionette.itemview中的某个地方,我可以做

template: NewAwesomeAngularTemplate.template.html // where NewAwesomeAngularTemplate.template.html is a file in my solution

Has anyone come across this yet? 有人遇到过这个吗? I wasn't able to find much info on how people were converting their apps to Angular from Backbone or Marionette. 我找不到太多有关人们如何将应用程序从Backbone或Marionette转换为Angular的信息。 Thanks in advance! 提前致谢!

Not too familiar with Backbone or Marionette, but went over and looked at the website. 对Backbone或Marionette不太熟悉,但走过去看了看网站。

I am not sure if this is what you are asking, so excuse me if I am way off base. 我不确定这是否是您要的内容,所以请问我是否离我们远了。 In Angular you can use the ngRouter or the more powerful uiRouter to bind your urls to controllers and views. 在Angular中,您可以使用ngRouter或更强大的uiRouter将您的URL绑定到控制器和视图。

state('index', {
  url: "/",
  controller: 'SomeController',
  templateUrl: "template/NewAwesomeAngularTemplate.html"
})

Not sure if this is what you were looking for. 不确定这是否是您想要的。 If so, check out the documentation at https://github.com/angular-ui/ui-router 如果是这样,请查看https://github.com/angular-ui/ui-router上的文档

You can use the grunt ts html2ts support : https://github.com/grunt-ts/grunt-ts#html-2-typescript-support 您可以使用grunt ts html2ts支持: https : //github.com/grunt-ts/grunt-ts#html-2-typescript-support

It turns an html file into a typescript string variable which you can return from the template function. 它将html文件转换为打字稿字符串变量,您可以从模板函数中返回该变量。

Marionette expects all templates to be immediately available, not having to be fetched async. Marionette希望所有模板都可以立即使用,而不必异步获取。 The view.template function can be easily overwritten but it needs to immediately return a string (or return a function that will immediately return a string). view.template函数很容易被覆盖,但是它需要立即返回一个字符串(或返回一个将立即返回一个字符串的函数)。

There was an old fork called Marionette.Async which enabled fetching of templates on-demand but it was abandonded long ago after the maintainers determined it to be A Really Bad Idea. 有一个叫做Marionette.Async的旧叉子,可以按需获取模板,但是很久以前,在维护人员将其确定为“真是个坏主意”之后就放弃了。

Your best bet is to build a mechanism that either loads all the templates into memory up front (like embedding them in a single JS file or the main HTML page) or a custom mechanism that ensures templates are fetched as-needed before the view gets used. 最好的选择是建立一种机制,将所有模板都预先加载到内存中(例如将它们嵌入到单个JS文件或HTML主页面中),或者使用一种自定义机制来确保使用视图之前按需提取模板。 。 The former will be much easier but viability depends on how many templates you have and how big they are. 前者会容易得多,但可行性取决于您拥有多少模板以及模板的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM