[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.

时间: 2024-08-11 21:22:03

[Angular Directive] 2. Add Inputs to Angular 2 Directives的相关文章

[Angular Directive] 3. Handle Events with Angular 2 Directives

A @Directive can also listen to events on their host element using @HostListener. This allows you to add behaviors that react to user input and update or modify properties on the element without having to create a custom component. import {Directive,

angular directive restrict 的用法

E 表示该指令是一个element; A 表示该指令是attribute; C 表示该指令是class; M 表示该指令是注视 实例如下: 原帖:www.thinkster.io/angularjs/rep5re7gTM/angularjs-directive-restrictions While it’s cool to make a custom element like we did the the previous cast, it’s actually more common to d

[Angular Directive] Implement Structural Directive Data Binding with Context in Angular

Just like in *ngFor, you're able to pass in data into your own structural directives. This is done by declaring the variable using a let statement then passing context into the createEmbeddedView call. We know *ngFor we did like this: *ngFor="let mes

[Angular Directive] Assign a Structual Directive a Dynamic Context in Angular 2

Just like passing in an array to *ngFor, you can pass in any value into your structural directive so that it can render templates based on those values. It's crucial to understand how the *directive syntax expands into a <template> and adds a custom

满脑子都是Angular/directive/scope/git

坑1: directive要用到controller里面的东西呢,有两种办法: 通过$scope.xxx来设置的,直接用xxx引用 通过controller function 里面的 this.xxx 设置的,要通过controllerName.xxx引用 爬爬墙,更健康:附AngularJS Directive文档 坑2: git rebase和git merge有啥不同? 看看这:rebase的用法 满脑子都是Angular/directive/scope/git

Angular项目构建指南 - 不再为angular构建而犹豫不决(转)

如果你不知道什么是Angular或者根本没听说过,那么我接下来所说的对你来说毫无益处,不过如果你打算以后会接触Angular或者干脆要涨涨姿势~读下去还是有点用的. Angular和它之前所出现的其余前端框架最大的不同,在于它的核心不再是DOM,而是数据,是model.我们惯用的不管是单纯的jQuery还是MVC的Backbone,它们本质仍是让我们更方便更有条理的操作DOM,但是Angular不是.通过一系列魔术般的手法,它将一切的重心转移到数据上.以开发应用而不是操作节点的方式去开发Web,

[Angular 2 Router] Configure Your First Angular 2 Route

Using the Angular 2 router requires defining routes, passing them in to the RouterModule.forRoot and then importing the configured RouterModule into your main App Module. Use the Wiki Search as example project. Create a HomeComponent to contain every

[从 0 开始的 Angular 生活]No.38 实现一个 Angular Router 切换组件页面(一)

前言 今天是进入公司的第三天,为了能尽快投入项目与成为团队可用的战力,我正在努力啃官方文档学习 Angular 的知识,所以这一篇文章主要是记录我如何阅读官方文档后,实现这个非常基本的.带导航的网页应用. 需求 需求大概是这样的: 开一个新的 Angular 项目,并且一开始选择加入 Router 功能 根组件是 AppComponent ,然后下方有三个子组件分别是 page1 page2 page3 可以在 AppComponent 内点击连结切换到这三个页面 参考文档: 路由与导航 Rou

[Angular Directive] Build a Directive that Tracks User Events in a Service in Angular 2

A @Directive is used to add behavior to elements and components in your application. This makes @Directives ideal for behaviors such as "tracking" which don't belong in a Component, but do belong as a behavior in your application. import {Direct