简体   繁体   English

Angular2:组件如何绑定到类?

[英]Angular2: how can component bind to class?

I can't find the obvious evidence that how a @Component() binds to a class. 我无法找到@Component()如何绑定到类的明显证据。 How can it know component binds with class RedditArticle instead of class Article ? 怎么知道组件与类RedditArticle而不是类Article绑定? After switching the position of the two class, it is messed up. 切换两个班级的位置后,它搞砸了。 Does that mean the class we need to bind should followed by the corresponding component? 这是否意味着我们需要绑定的类应该跟着相应的组件?

import { bootstrap } from "angular2/platform/browser";
import { Component } from "angular2/core";

@Component({
    selector: 'reddit-article',
    template: `
        <div class="row">
            <div class="col-md-3"></div>
                <div>Points: {{article.votes}}</div>
            <div class="col-md-9"></div>
                <!--<div class="row">-->
                    Title: {{article.title}}
                    Link: {{article.link}}
                    <button (click)="voteUp()">upvote</button>
                    <button (click)="voteDown()">downvote</button>
                <!--</div>-->
        </div>
    `
})

class RedditArticle {

    article: Article;

    constructor() {
        this.article = new Article('angular2', 'google.com', 0);
    }

    voteUp() {
        this.article.votes++;
    }

    voteDown() {
        this.article.votes--;
    }
}

class Article {
    title: string;
    link: string;
    votes: number;

    constructor(title: string, link: string, votes: number) {
        this.title = title;
        this.link = link;
        this.votes = votes;
    }
}

The @Component() decorator applies directly to the class that follows the annotation. @Component()装饰器直接应用于注释@Component()的类。

This is the same for all annotations. 对于所有注释,这都是相同的。

For example 例如

constructor(@Inject('xxx') private val:string, private zone:NgZone) {}

Here @Inject() is bound to val 这里@Inject()绑定到val

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM