如何在Angular 2中将值从父组件传递给指令? [英] how can I pass a value from parent component to directive in Angular 2?

查看:59
本文介绍了如何在Angular 2中将值从父组件传递给指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的父组件中包含以下代码行

If I have the following line of code in my parent component

<input datepicker type="text" (change)="update($event)"/>

如何将值传递给datepicker指令?

How can I pass a value to the datepicker directive?

推荐答案

在指令中定义输入

datepicker.directive.ts

import {Input} from '@angular/core';
...
export class DatePickerDirective{
    @Input() config:any = {}; //set to default configuration
}

现在,如果您绑定到模板中此指令的 config 属性,Angular将传递该值

Now if you bind to the config property of this directive in the template, Angular will pass that value

parent.component.ts

dateConfig:any;

parent.component.html

<input datepicker type="text" [config]="dateConfig"/>

这篇关于如何在Angular 2中将值从父组件传递给指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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