简体   繁体   English

在Durandal中插入脚本标记

[英]Insert script tag in Durandal

I am a bit struggling with what should be an easy task. 我有点挣扎应该是一件容易的事。 I want to insert a script tag inside a div tag. 我想在div标签中插入一个脚本标签。 The problem is that Durandal (for a reason that I still need to understand) seems to ignore those tags. 问题是Durandal(我仍然需要了解的原因)似乎忽略了那些标签。 This problem has been described here, but the solution proposed there does not work for me. 这里已经描述了这个问题,但是那里提出的解决方案对我不起作用。 how to stop durandal / jquery stripping out script tags from views? 如何阻止durandal / jquery从视图中删除脚本标记?

Isn't there a simple way to tell Durandal to please interpret my script tags? 是不是有一种简单的方法告诉Durandal请解释我的脚本标签? This is becoming very frustrating, and besides the above question, I couldn't find anything on the web about this. 这变得非常令人沮丧,除了上面的问题,我在网上找不到任何关于此的内容。

Thanks 谢谢

Nicolas 萨科

Ok, here's what I do to get this to work. 好的,这就是我要做的工作。 Inside the parent tag, I use a compose binding like so: 在父标签内部,我使用compose绑定,如下所示:

<td>
     <!-- ko compose: { view: 'views/homearea', model: 'viewmodels/homearea' } -->
     <!-- /ko-->
</td>

Inside my view, I have something like this: 在我看来,我有这样的事情:

<div id="homearea" style="height: 400px; width: 400px;">    
</div>

And inside the model, in the viewAttached method, I can put my custom javascript. 在模型内部,在viewAttached方法中,我可以放置我的自定义javascript。

define(function () {

    var viewAttached = function () {
        $('#homearea').myFunction();
    };

    var vm = {
        viewAttached: viewAttached
    };
    return vm;
});

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

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