首页 > 代码库 > [Angular Directive] 2. Add Inputs to Angular 2 Directives
[Angular Directive] 2. Add Inputs to Angular 2 Directives
The @Input
decorator allows you to pass values into your @Directive
so that you can change the value of the Directive each time that it is used. Using @Input
makes your Directives much more flexible and reusable so they can adapt to many different situations.
import {Directive, Input, HostBinding} from ‘@angular/core‘;@Directive({ selector: ‘[getInput]‘})export class GetInputDirective { @Input(‘getInput‘) input; @HostBinding() get innerText() { return this.input; } constructor() { }}
<span [getInput]="‘something‘">I am a span</span>
[getInput] means we have a prop on our directive call ‘getInput‘, go and find it and set the value as ‘something‘.
It will only show "something" on the page. Here we pass value to the directive, then reflect the input value to the Host element‘s innerText by using getter.
[Angular Directive] 2. Add Inputs to Angular 2 Directives
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。