2015-07-20 190 views
2

我想獲得一個簡單的動態視圖工作的最簡單的方法。動態視圖創建

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>'); 

但apparantly我失去了一些東西,認爲工廠應該能夠創建一個視圖,那麼它應該被添加到viewslot?

我在上面的代碼中缺少什麼?

我要指出,我試圖按照羅布的這個線程評論: https://github.com/aurelia/templating/issues/35

回答

2

看到更新後的羅布在同一thread

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(); 
    } 
} 
+0

是的,我注意到了,不知道這是否足夠。一旦我測試了它,我也會在這裏發佈結果。 – Janus007

1

遲到的答案,但這可以很容易地通過使用由aurelia提供的getViewStartegy方法實現。

export class Test { 

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