官网:https://angular.io/guide/architecture-components

Before a view is displayed, Angular evaluates the directives and resolves the binding syntax in the template to modify the HTML elements and the DOM, according to your program data and logic. Angular supports two-way data binding, meaning that changes in the DOM, such as user choices, are also reflected in your program data.

view显示之前,Angular评估页面模板里的Angular指令,解析绑定信息,修改HTML元素和DOM. Angular支持双向绑定,这意味着发生在DOM上的修改,比如用户选择,也会自动反映到应用程序的数据中。

服务注入

A service class definition is immediately preceded by the @Injectable() decorator. The decorator provides the metadata that allows other providers to be injected as dependencies into your class.

Angular Service的定义通过装饰器@Injectable()来完成,提供了元数据,以便让其他provider通过依赖的方式注入。

Module

通过装饰器@NgModule()定义。一个例子:

@NgModule({declarations: [AppComponent],imports: [BrowserModule,AppRoutingModule],providers: [],bootstrap: [AppComponent]
})

包含下列的部分:

  • declarations: The components, directives, and pipes that belong to this NgModule.

  • imports: Other modules whose exported classes are needed by component templates declared in this NgModule.

  • exports: The subset of declarations that should be visible and usable in the component templates of other NgModules.

A root NgModule has no reason to export anything because other modules don’t need to import the root NgModule.

The components that belong to an NgModule share a compilation context.

NgModule和Component的关系是1:N关系,一对多。

In JavaScript each file is a module and all objects defined in the file belong to that module. The module declares some objects to be public by marking them with the export key word. Other JavaScript modules use import statements to access public objects from other modules.

每个JavaScript文件是一个module,所有定义在该文件里的对象都属于该module. Module使用export关键字将对象中的一部分声明为公有。JavaScript module使用import来导入其他module的公有对象。

Angular libraries

通过@angular前缀声明Angular libraries.

例子:For example, import Angular’s Component decorator from the @angular/core library like this.

import { Component } from ‘@angular/core’;

Component

看个例子:

  • selector: A CSS selector that tells Angular to create and insert an instance of this component wherever it finds the corresponding tag in template HTML. For example, if an app’s HTML contains , then Angular inserts an instance of the HeroListComponent view between those tags.

有点像SAP UI5的index.html里定义的place holder

  • templateUrl: The module-relative address of this component’s HTML template. Alternatively, you can provide the HTML template inline, as the value of the template property. This template defines the component’s host view.

Angular模板里的数据绑定语法

记住这张图:

看个例子:

  • {{hero.name}}: 将Component的hero.name property(属性值)显示到li标签里。

  • [hero]: The [hero] property binding passes the value of selectedHero from the parent HeroListComponent to the hero property of the child HeroDetailComponent.

将selectedHero的值从HeroListComponent传递到HeroDetailComponent的hero属性去。

  • (click): The (click) event binding calls the component’s selectHero method when the user clicks a hero’s name. 当用户点击hero的name时,调用component的selectHero方法。

双向绑定的语法:<input [(ngModel)]=“hero.name”>

Two-way data binding (used mainly in template-driven forms) combines property and event binding in a single notation.

In two-way binding, a data property value flows to the input box from the component as with property binding. The user’s changes also flow back to the component, resetting the property to the latest value, as with event binding.

Angular Directive

Angular templates are dynamic. When Angular renders them, it transforms the DOM according to the instructions given by directives. A directive is a class with a @Directive() decorator.

Angular template是动态的,当模板被渲染时,根据模板里包含的指令来转换DOM. 一个Angular directive就是一个被@Directive()修饰的class.

Structual directive

Structural directives alter layout by adding, removing, and replacing elements in the DOM.

Structure directive通过对DOM里的元素进行增加,删除和替换的操作来影响界面布局。

attribute directive

Attribute directives alter the appearance or behavior of an existing element. In templates they look like regular HTML attributes, hence the name.

影响一个已有element的外观或者行为,在Angular模板里看起来和普通的HTML属性类似,故得名。

Attribute directive的一个例子:The ngModel directive, which implements two-way data binding, is an example of an attribute directive. ngModel modifies the behavior of an existing element (typically ) by setting its display value property and responding to change events.

<input [(ngModel)]=“hero.name”>

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

Angular component的一个例子相关推荐

  1. SAP Spartacus 服务器端 nodejs 应用里渲染 Angular Component 的一个例子

    触发方式:将下列 url 粘贴到浏览器地址栏里,回车: http://localhost:4000/Open-Catalogue/Cameras/Hand-held-Camcorders/c/584 ...

  2. Webpack入门——使用Webpack打包Angular项目的一个例子

    (一)什么是Webpack Webpack是一个前端的模块管理工具(module bundler),以下是webpack的官网:http://webpack.github.io/,一进入官网可以看到下 ...

  3. Angular NgTemplateOutlet的一个例子

    NgTemplateOutlet:Inserts an embedded view from a prepared TemplateRef. 语法: <ng-container *ngTempl ...

  4. Angular bootstrap的一个例子

    A way to initialize and launch an app or system. In Angular, an app's root NgModule (AppModule) has ...

  5. Angular 自定义 structural 指令的一个例子

    The <ng-container> is a syntax element recognized by the Angular parser. It's not a directive, ...

  6. SAP Spartacus 中,Angular <ng-container>使用的一个例子

    Angular官网里对ng-container的介绍: The Angular is a grouping element that doesn't interfere with styles or ...

  7. Angular Component的DOM级别的单元测试方法

    HTMLElement <span [textContent] = "AText"></span> export class AComponent {ATe ...

  8. 从 Angular Component 和 Directive 的实例化,谈谈 Angular forRoot 方法的命令由来

    同 Angular service 的单例特性不同,Angular 组件和指令通常会被多次实例化,比如 HTML markup 中每出现一次 Component 的 selector,就会触发 Com ...

  9. 深入解析Angular Component的源码示例

    本篇文章主要介绍了剖析Angular Component的源码示例,写的十分的全面细致,具有一定的参考价值,对此有需要的朋友可以参考学习下.如有不足之处,欢迎批评指正. Web Component 定 ...

最新文章

  1. uvm_dpi——DPI在UVM中的实现(一)
  2. 用python将xml文件转换为txt文件_python代码xml转txt实例
  3. 下面不属于python第三方库的安装方法的是-Python第三方库安装和卸载
  4. 启动项目的时候,一直打印日志
  5. python3.7界面设计_基于selenium+Python3.7+yaml+Robot Framework的UI自动化测试框架
  6. Chillingo分享三款游戏图标的设计经验
  7. 作者:许会泉,男,北京金信网银金融信息服务有限公司研发总监。
  8. vSphere 6.5 High Availability New Features – Proactive HA(vSphere 6.5高可用性新功能–主动HA)
  9. jspSmartUpload 组件的使用方法
  10. 【Opencv卸载与重装】NVIDIA Xavier NX下,卸载opencv3,重装opencv4
  11. Codeforces 1071 C - Triple Flips
  12. 搜狐的本色和张朝阳的本分
  13. Logistic Regression 逻辑回归 简单易懂的笔记 by hch
  14. 论文翻译--毫米波三维全息摄影的轻量级FMIST启发稀疏重建网络(1)
  15. 什么是作用域和作用域链以及闭包?
  16. 同城物流附近的物流公司电话源码
  17. Android 13 NavigationBar
  18. spring boot 工程 Maven 打包总结
  19. 做职业监理师(九)——监理方如何审核《需求规格说明书》
  20. 怎样批量制作二维码标签?

热门文章

  1. 学习PHP时的一些总结(五)
  2. win64环境下的一些配置
  3. openstack安装rabbitmq-server消息队列时出现错误
  4. 【Asp.Net】一般处理程序(ashx处理程序) 和 aspx页面处理程序
  5. 2018-2019年度总结
  6. 方立勋_30天掌握JavaWeb_JSTL标签库
  7. vue.$nextTick 解决了哪些问题
  8. php获取用户的上5级用户
  9. 11月18日站立会议
  10. 成功抓取豆瓣读书的所有书籍