- Switching to Angular(Third Edition)
- Minko Gechev
- 337字
- 2021-07-02 15:23:32
Components in Angular
Angular took this approach by introducing new building blocks called components. Components extend the directive concept we described in the previous section and provide a broader functionality. Here is the definition of a basic Hello world component:
@Component({ selector: 'hello-world', template: '<h1>Hello, {{target}}!</h1>' }) class HelloWorld { target: string; constructor() { this.target = 'world'; } }
We can use it by inserting the following markup in our view:
<hello-world></hello-world>
We will take a look at the preceding syntax in more detail later in this book. Now let's briefly describe the functionality that this component provides. Once the Angular application is bootstrapped, it will look at all the elements in the DOM tree and process them. When it finds an element called hello-world, it will invoke the logic associated with its definition, which means that the template of the component will be rendered and the expression between the curly brackets will be evaluated. This will result in the <h1>Hello, world!</h1> markup.
So, to summarize, the Angular core team separated out the directives from AngularJS into two different parts: components and directives. Directives provide an easy way to attach behavior to the DOM elements without defining a view. Components in Angular provide a powerful, and yet simple-to-learn API, which makes it easier to define the user interface of our applications. Angular components allow us to do the same amazing things as AngularJS directives, but with less typing and fewer things to learn. Components extend the Angular directive concept by adding a view to it. We can think of the relation between Angular components and directives the same way as the relation between Composite and Leaf from the diagram we saw in Figure 5.
Conceptually, we can present the relation between directives and Components as inheritance. Chapter 5, Getting Started with Angular Components and Directives, describes these two concepts in further detail.
- C語言程序設(shè)計(第3版)
- Oracle Database In-Memory(架構(gòu)與實踐)
- Oracle 12c中文版數(shù)據(jù)庫管理、應(yīng)用與開發(fā)實踐教程 (清華電腦學(xué)堂)
- 大學(xué)計算機基礎(chǔ)(第2版)(微課版)
- 你不知道的JavaScript(中卷)
- 自然語言處理Python進階
- 輕松上手2D游戲開發(fā):Unity入門
- 響應(yīng)式Web設(shè)計:HTML5和CSS3實戰(zhàn)(第2版)
- Microsoft 365 Certified Fundamentals MS-900 Exam Guide
- Mudbox 2013 Cookbook
- 零基礎(chǔ)學(xué)SQL(升級版)
- 分布式數(shù)據(jù)庫HBase案例教程
- 區(qū)塊鏈:技術(shù)與場景
- 少年小魚的魔法之旅:神奇的Python
- 微信公眾平臺服務(wù)號開發(fā):揭秘九大高級接口