Angular 角度2-如何通过;勾选“;模板中的子级属性?

Angular 角度2-如何通过;勾选“;模板中的子级属性?,angular,angular2-template,angular2-directives,Angular,Angular2 Template,Angular2 Directives,我有一个自定义指令。在foo的模板中,我有。如果选中了并启用了foo,则我想要。我还没有看到如何处理这些没有值的属性,检测它们,并在它们存在时用它们标记子元素 非常感谢您的帮助。请尝试以下操作: 这里还有一个plunker: 从'angular2/core'导入{Component,Attribute} @组成部分({ 选择器:'子', 提供者:[], 模板:` `, 指令:[] }) 导出类子{ 检查; 构造函数(@Attribute(“checked”)已选中){ this.isChecke

我有一个自定义指令
。在
foo
的模板中,我有
。如果选中了
并启用了
foo
,则我想要
。我还没有看到如何处理这些没有值的属性,检测它们,并在它们存在时用它们标记子元素

非常感谢您的帮助。

请尝试以下操作:

这里还有一个plunker:

从'angular2/core'导入{Component,Attribute}
@组成部分({
选择器:'子',
提供者:[],
模板:`
`,
指令:[]
})
导出类子{
检查;
构造函数(@Attribute(“checked”)已选中){
this.isChecked=!(checked==null);
}
}
@组成部分({
选择器:“我的应用程序”,
提供者:[],
模板:`
`,
指令:[儿童]
})
导出类应用程序{
}
尝试以下操作:

这里还有一个plunker:

从'angular2/core'导入{Component,Attribute}
@组成部分({
选择器:'子',
提供者:[],
模板:`
`,
指令:[]
})
导出类子{
检查;
构造函数(@Attribute(“checked”)已选中){
this.isChecked=!(checked==null);
}
}
@组成部分({
选择器:“我的应用程序”,
提供者:[],
模板:`
`,
指令:[儿童]
})
导出类应用程序{
}

我认为根据您的要求,在指令构造函数中您可以有:-

Foo指令-

import {Directive, Attribute} from 'angular2/core';
@Directive({
    selector:   'foo'
})
export class foo {
     constructor(@Attribute('checked') checked: string) {
            if (checked === null){
              //checked is not present
            }else{
              //checked is present
            }
...
    }
....
}
而且,你可以使用-

<foo checked></foo>

我认为根据您的要求,在指令构造函数中您可以有:-

Foo指令-

import {Directive, Attribute} from 'angular2/core';
@Directive({
    selector:   'foo'
})
export class foo {
     constructor(@Attribute('checked') checked: string) {
            if (checked === null){
              //checked is not present
            }else{
              //checked is present
            }
...
    }
....
}
而且,你可以使用-

<foo checked></foo>