码迷,mamicode.com
首页 > 其他好文 > 详细

[Angular Directive] 2. Add Inputs to Angular 2 Directives

时间:2016-12-21 07:46:40      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:add   export   decorator   import   situation   angular   can   val   value   

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

标签:add   export   decorator   import   situation   angular   can   val   value   

原文地址:http://www.cnblogs.com/Answer1215/p/6206205.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!