首页 > 其他 > 详细

[Angular Directive] 1. Write an Angular Directive

时间:2016-12-21 07:39:49      阅读:279      评论:0      收藏:0      [点我收藏+]

Angular 2 Directives allow you manipulate elements by adding custom behaviors through attributes. This lesson covers how to create a Directive and attach its behavior to an element.

import {Directive, HostBinding} from @angular/core;

@Directive({
  selector: [myText]
})
export class TextDirective {

  @HostBinding() innerText;
  constructor() {
    this.innerText = "I am text directive!"
  }
}

 

There are tow things important here:

  • selector: ‘[myText]‘, this is an attr selector.
  • HostBinding: Bind to host element‘s props.

If we using like this:

<div myText>I am a div</div>
<span>I am a span</span>
<hr>
<span myText>My text will be changed!</span>

This directive will change div and last span‘s innerText to ‘I am text directive!‘.

[Angular Directive] 1. Write an Angular Directive

原文:http://www.cnblogs.com/Answer1215/p/6206203.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!