2016-09-20 57 views
0

我想知道某个组件是否设置了[(ngModel)]检测组件是否在Angular 2中设置了NgModel

我正在创建一个typeahead组件,并且想知道ngModel是否设置为元素。

事情是这样的:

我-form.component.html

... 
<my-custom-typeahead [(ngModel)]="somevariable" name="someinput1"><my-custom-typeahead> 

<my-custom-typeahead name="someinput2"><my-custom-typeahead> 
... 

我定制-typeahead.component.ts

@ngModule({ 
    ... 
    selector: 'my-custom-typeahead', 
    ... 
}) 
export class myCustomTypeaheadComponent{ 
    // I would like to know if the ngModel was set in the component 
    // something like @Host('my-custom-typeahead').has2wdb('ngModel') 
} 

我认为使用@host不是答案,但这是我可以更多地解释它的方式。

+0

请澄清你的问题。也许添加一些代码..不能关注.. – mxii

+0

@mxii我编辑了我的问题,并提供了我想要做的一个例子。谢谢你的时间! – vinagreti

回答

1

如果你想实现[(ngModel)]你必须实现它的接口ControlValueAccessor

该接口具有的功能writeValue(val: any) ..

如果这个函数被调用,您通过[(ngModel)]接收的值。 :)

相关问题