将动态html插入durandal视图中 [英] inserting dynamic html into durandal views

查看:63
本文介绍了将动态html插入durandal视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在动态地在durandal视图中插入html,但是这样做不行吗?我的示例html是

I am dynamically inserting html in durandal views but it does'nt work can any one help me out in that? my sample html is

在正统视图中

<div data-bind="foreach:arrayofmodels">
    <div data-bind="html:dynamichtml"/>
  </div>

在要插入动态html的模型(不是视图模型)中

in my model(not view model) which is going to insert dynamic html

dynamichtml(<span data-bind="click:Mymodelclickfunction"/>);

在我的视图模型中

    var arrayofmodels=ko.observableArray();
     vm={
         arrayofmodels:arrayofmodels;
       }
return vm;

有一个单独的函数来获取模型对象列表并将其推入 arrayofmodels 数组.

there is a separate function to get list of model objects and push them into arrayofmodels array .

我了解问题是在应用绑定后插入html,但是如何解决此问题?

I understand the problem is inserting html after bindings were applied, but how to solve this issue?

推荐答案

已更新:

您是否考虑过为此目的使用Durandal的撰写机制?假设模型返回具有Mymodelclickfunction属性的构造函数,那么下面的内容应该会让您入门.

Have you considered using Durandal's compose mechanism for that purpose? Assuming that the model returns a constructor function with a Mymodelclickfunction property something along the following should get you started.

model.js

define(function() {
    return function() {
        this.Mymodelclickfunction = function(){
           //...
        }
    };
});

model.html

model.html

<div>
    <span data-bind="click:Mymodelclickfunction"/>
</div>

基于评论进行更新 durandal视图

Update based on comment durandal view

<div data-bind="foreach:arrayofmodels">
    <!-- ko compose: $data -->
    <--/ko -->
</div>

看看 http://dfiddle的view-composition部分. github.io/dFiddle-1.2/#/view-composition 来查看更多示例.

这篇关于将动态html插入durandal视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆