动态视图创建

时间:2015-07-20 22:36:48

标签: aurelia

我试图以最简单的方式使用简单的动态视图。

import {inject, noView, ViewCompiler, ViewSlot} from 'aurelia-framework';

@noView
@inject(ViewCompiler, ViewSlot)
export class ButtonVM{

    constructor(vc, vs){

        this.viewCompiler = vc;
        this.viewSlot = vs;


        var viewFactory =  this.viewCompiler.compile('<button>Some button</button>');

但显然我错过了一些东西,视图工厂应该能够创建一个视图然后它应该被添加到一个视图中吗?

上述代码中缺少什么?

我应该提一下,我试图在这个帖子中关注Rob的评论: https://github.com/aurelia/templating/issues/35

2 个答案:

答案 0 :(得分:3)

在同一thread中查看Rob发布的最新帖子。

import {inject, noView, ViewCompiler, ViewSlot, Container, ViewResources} from 'aurelia-framework';

@noView
@inject(ViewCompiler, ViewSlot, Container, ViewResources)
export class Test {
    constructor(vc, vs, container, resources){
        this.viewCompiler = vc;
        this.viewSlot = vs;

        var viewFactory =  this.viewCompiler.compile('<button>${title}</button>', resources);
        var bindingContext = { title:'Click Me'};
        var view = viewFactory.create(container, bindingContext);
        this.viewSlot.add(view);
        this.viewSlot.attached();
    }
}

答案 1 :(得分:1)

答案迟到但是这可以通过使用aurelia提供的getViewStartegy方法轻松实现。

export class Test {

 constructor(vc, vs, container, resources){
 }
 getViewStrategy() {
    return new InlineViewStrategy('<button>${title}</button>');
 }
}