zoukankan      html  css  js  c++  java
  • Angular2 Decorator

    1 at first, we use the (keyup) event handler to update the modify

    @Component({
        selector: 'my-app'
    })
    
    @Template({
        inline: '<h1>{{myName}}</h1>' +
        '<input type="text" #newname (keyup)/>' +
        '<h3 [style.color]="newname.value">{{newname.value}}</h3> '
    
    })
    

    but, if i wanna do something more when I use the keyup in all the input keyup?

    we use the Decorator to decorator the event

    2 import the Decorator

    import {Component, Template, bootstrap, Decorator} from 'angular2/angular2';
    

    3 define the Decorator

    @Decorator({
        selector: 'input',
        events: {'keyup': 'onKeyUp()'}
    })
    
    class InputDecorator {
        onKeyUp() {
            console.log('do nothing');
        }
    }
    

    4 use the decorator

    @Template({
        inline: '<h1>{{myName}}</h1>' +
        '<input type="text" #newname />' +
        '<input type="text" #test/>' +
        '<h3 [style.color]="newname.value">{{newname.value}}</h3> ',
        directives: [InputDecorator]
    
    })
    

    5 the whole page code

    import {Component, Template, bootstrap, Decorator} from 'angular2/angular2';
    
    @Decorator({
        selector: 'input',
        events: {'keyup': 'onKeyUp()'}
    })
    
    class InputDecorator {
        onKeyUp() {
            console.log('do nothing');
        }
    }
    
    @Component({
        selector: 'my-app'
    })
    
    @Template({
        inline: '<h1>{{myName}}</h1>' +
        '<input type="text" #newname />' +
        '<input type="text" #newnam />' +
        '<h3 [style.color]="newname.value">{{newname.value}}</h3> ',
        directives: [InputDecorator]
    
    })
    
    class MyApp {
    
        constructor() {
            this.myName = 'Jackey';
            this.myFriends = [
                {name: 'Jackey1', age: 25},
                {name: 'Jackey2', age: 26}
            ];
        }
    }
    
    bootstrap(MyApp);
    

      

  • 相关阅读:
    操盘策略:判断强庄股的四个诀窍
    三类股有望继续走强
    操盘策略:股市空头陷阱五大招数
    每日一招:面对亏损我们应该如何操作(鳄鱼法则)
    (转)一个大户的自白:我是这样被两融打爆的
    3.2、迭代
    3.1、切片
    2.4、递归函数
    2.3、函数的参数
    2.2、定义函数
  • 原文地址:https://www.cnblogs.com/lihaozhou/p/4663067.html
Copyright © 2011-2022 走看看