zoukankan      html  css  js  c++  java
  • [Angular 2] Passing data to components with 'properties'

    Besides @Input(), we can also use properties on the @Component, to pass the data.

    import {Component, View, NgFor, Input} from 'angular2/angular2';
    
    @Component({
        selector: 'reddit-article'
    })
    @View({
        directives: [],
        template: `
            <article>
                <div class="votes">{{article.votes}}</div>
                <div class="main">
                    <h2>
                        <a href="{{article.link}}">{{article.title}}</a>
                        <span>({{ article.domain() }})</span>
                    </h2>
                    <ul>
                        <li><a href (click)="article.voteUp()">Up</a></li>
                        <li><a href (click)="article.voteDown()">Down</a></li>
                    </ul>
                </div>
            </article>
        `
    })
    
    export class RedditArticle {
        @Input() article: Article;
    
        voteUp() {
            this.article.voteUp();
            return false;
        }
    
        voteDown() {
            this.article.voteDown();
            return false;
        }
    }

    Works the same as:

    import {Component, View, NgFor, Input} from 'angular2/angular2';
    
    @Component({
        selector: 'reddit-article',
        properties: ['article']
    
    })
    @View({
        directives: [],
        template: `
            <article>
                <div class="votes">{{article.votes}}</div>
                <div class="main">
                    <h2>
                        <a href="{{article.link}}">{{article.title}}</a>
                        <span>({{ article.domain() }})</span>
                    </h2>
                    <ul>
                        <li><a href (click)="article.voteUp()">Up</a></li>
                        <li><a href (click)="article.voteDown()">Down</a></li>
                    </ul>
                </div>
            </article>
        `
    })
    
    export class RedditArticle {
        article: Article;
    
        voteUp() {
            this.article.voteUp();
            return false;
        }
    
        voteDown() {
            this.article.voteDown();
            return false;
        }
  • 相关阅读:
    PHP定时执行计划任务
    MySQL正则表达式 REGEXP详解
    mysql常用的一些命令,用于查看数据库、表、字段编码
    MySQL 编码
    【MySQL】Win7下修改MySQL5.5默认编码格式
    linux下使用svn
    MySql command line client 命令系列
    linux svn
    BZOJ5317 JSOI2018部落战争(凸包)
    Educational Codeforces Round 58 Div. 2 自闭记
  • 原文地址:https://www.cnblogs.com/Answer1215/p/4929130.html
Copyright © 2011-2022 走看看