是否有可能增加使用BrowserDomAdapter局部模板变量? [英] Is it possible to add a local template variable using BrowserDomAdapter?

查看:260
本文介绍了是否有可能增加使用BrowserDomAdapter局部模板变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

枝......有没有一种方法,我可以localtemplate变量设置为创建的元素。因为我想创建的元素应该有一个localtemplate变量。我能够设置id因为它是一个属性,但不是localtemplate variable.I已经取得了plunker演示的 http://plnkr.co/edit/VGHEPBs8oZW7HG6Ze21z?p=$p$pview ,我试图通过以下方式来创建它。

Hie...Is there a way i can set localtemplate variable to the element created. Because I want the element created should have a localtemplate variable. I am able to set the id since it is an attribute but not localtemplate variable.I have made a plunker demo http://plnkr.co/edit/VGHEPBs8oZW7HG6Ze21z?p=preview And i am trying to create it in the following way

add(){
var a=this.dom.createElement('button');
var b=this.dom.createTemplate('<div #child></div>');
this.dom.on(a, 'click', this.raiseevent.bind(this, this.op));
this.dom.setInnerHTML(a,'clickme');
this.dom.appendChild(this.dom.query("my-app"),a);
this.dom.appendChild(this.dom.query("my-app"),b);
this.op++;

}

意味着它的类似于一个ID,我可以在任何地方查看使用它......它会是这样的,其中#child是本地模板变量。 子前的#preFIX标识子作为局部模板变量。我们可以在模板中引用此变量来访问子项的属性。

Means its similar to an id which i can use it anywhere in view...It will be something like this where #child is the local template variable. The # prefix before "child" identifies the child as a local template variable. We can reference this variable within the template to access a child's properties

推荐答案

为什么你需要注入的DOM元素,同样可以使用组件来实现?

Why do you need to inject the DOM elements in, the same can be accomplished with components?

@Component({ 
    selector: 'child',
    template: `<button (click)="raiseevent()">clickme</button>`
})
export class ChildComponent{
    raiseevent(){
        // do something
    }
}

@Component({
    selector: 'my-app',
    template: `<child #child *ngFor="#item in children"></child>`,
    directives: [ChildComponent]
})
export AppComponent{
    children: any[] = [];

    add(){
        this.children.push({...});
    }
}

最终的结果将是相同的。每次添加()方法被调用时,一个新的按钮将与它自己的的RaiseEvent()方法添加。你甚至可以有一个孩子用火的方法对母的 EventEmitter

The end result will be the same. Each time the add() method is called, a new button will be added with it's own raiseevent() method. You can even have a child fire a method on the parent using an EventEmitter.

这并没有考虑到你的&LT; D​​IV#孩子&GT;&LT; / DIV&GT; ,你是注射的,因为我不知道你的意图是用什么。如果能提供一个更详细的例子,我可以提供更完整的答案。虽然你可以很容易地添加&LT; D​​IV&GT; 模板 ChildComponent 和最终的结果会是一样的。

This does not take into account your <div #child></div> that you are injecting in because I do not know what your intentions are with that. If you could provide a more detailed example I could provide a more thorough answer. Though you could just as easily add the <div> to the template of ChildComponent and the end result would be the same.

这篇关于是否有可能增加使用BrowserDomAdapter局部模板变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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