[Angular Directive] 1. Write an Angular Directive

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!‘.

时间: 2024-11-07 22:32:47

[Angular Directive] 1. Write an Angular Directive的相关文章

angular学习(十二)—— Directive

directive介绍 directive是DOM元素上的标记,告诉angularjs的HTML编译器($complile)给DOM元素附加上一些特殊的行为,或者是改变DOM元素和它的子元素. 看到编译两个字,很多人会感到很懵,javascript不是解释执行的吗.其实这里的编译是因为,给html附加directive的递归过程很像是编译源代码的过程,所以才叫编译. angularjs内置了一套directive,像ngBind, ngModel和ngClass.就像你创建controller和

angular的跨域(angular百度下拉提示模拟)和angular选项卡

1.angular中$http的服务: $http.get(url,{params:{参数}}).success().error(); $http.post(url,{params:{参数}}).success().error(); $http.jsonp(url,{params:{wd:'',cb:'JSON_CALLBACK'}}).success().error(); 注意jsonp中cb在angular中规定只能使用JSON_CALLBAC $watch(谁,做什么,false): 谁指

【angular.js】UI-Router之angular路由学习

AngularJs中的路由,应用比较广泛,主要是允许我们通过不同的url访问不同的内容,可实现多视图的单页web应用.下面看看具体怎么使用. 关于路由 通常我们的URL形式为http://jtjds.cn/first/page,但在单页web应用中angularjs通过#+标记实现,比如下面的页面,将是下文中的路由列子. http://192.168.1.109:8000/angular-program/src/main.html#/pagetable/page1 http://192.168.

简话Angular 01 为什么要学Angular

1. JQuery vs Javascipt 问两个问题: 1) 你用过JQuery吗?当然! 2) 感觉JQuery相对于原生纯Javascript优势在哪里? 我的答案:JQuery更直接方便,兼容性好,代码少,好理解,快速开发,Bug少…… 2. Angular vs JQuery 参照JQuery vs Javascript 3. Angular的前途怎么样 假如今天是2008年,有人问JQuery前途怎么样,你知道吗?现在你知道JQuery前途了吗? 4. Angular可以不学吗 不

[Angular 2] BYPASSING PROVIDERS IN ANGULAR 2

Artical --> BYPASSING PROVIDERS IN ANGULAR 2 Here trying to solve one problem: On the left hand side of tree, there are 4 green blocks and 1 blue block. Meaning that three green dataService will use 'OtherProvider' which in an instance of DataService

[Angular 2] Generate and Render Angular 2 Template Elements in a Component

Angular 2 Components have templates, but you can also create templates inside of your templates using Angular 2 ViewContainer’s createEmbeddedView for more visual control of how you want to generate elements inside of your Angular 2 templates. import

在Angular外部使用js调用Angular控制器中提供的函数方法或变量

Html代码如下所示: 1 <!DOCTYPE html> 2 <html ng-app="myApp" id="myApp"> 3 <head> 4 <meta name="viewport" content="width=device-width" /> 5 <title>Test</title> 6 <script src="~/Co

[Angular 2] Move and Delete Angular 2 Components After Creation

After the original order is set, you can still leverage methods on the Angular 2 ViewContainer to reorder the components. This is especially helpful when you want an event to trigger layout changes from your generated components. import { Component,

[Angular] Handle HTTP Errors in Angular with HttpErrorResponse interface

When communicating with some backend API, data travels over the network using the HTTP protocol. As such, failures may occur, be it on our own device (i.e. the browser) or on the server-side which may not be available or unable to process our request