Angular --基本构造
来源:互联网 发布:东方财富dk买卖点源码 编辑:程序博客网 时间:2024/06/03 18:06
官网已经很清楚了,在这里记录下,是为了学习更方便。
本文内容来源angular官网
转载请注明出处我的博客http://www.lostbug.cn
简介
Angular 是一个用HTML和JavaScript或者一个可以编译成JavaScript的语言(TypeScript .el),来构建客户端应用的框架。
Angular主要构造块
- Modules
- Components
- Templates
- Metadata
- Data binding
- Directives
- Services
- Dependency injection
Modules
Angular apps are modular and Angular has its own modularity system called Angular module or NgModules.
Every Angular app has at least one Angular module class, the root module,conventionally named AppModule.
One Angular app may have no or many feature modules,
每个模块都是一个内聚的代码块,专注于某个应用领域、工作流或紧密相关的功能。
An Angular module,whether a root or feature,is a class with an @NgModule
decorator.
NgModule is a decorator function that takes single metadata object whose properties describe the module.
The most important properties are:
declarations
- the view classes that belong to this module.Angular has three kinds of view classes: components,directives, and pipes.exports
-the subset of declarations that should be visible and usable in the componenttemplates
of other modules.imports
-other module whose exported classes are needed by component templates declared inthis
module.providers
-creators ofservices
that this module contributs to the global collection of service;they become accessible in all parts of the app.bootstrap
-the main application view,called the root component,that hosts all other app views.Only the root module should set thisbootstrap
property.
Example:
import { NgModule } from '@angular/core';import { BrowserModule } from '@angular/platform-browser';@NgModule({ imports: [ BrowserModule ], providers: [ Logger ], declarations: [ AppComponent ], exports: [ AppComponent ], bootstrap: [ AppComponent ]})export class AppModule { }
Launch an application by bootstrapping its root module.During development you’re likely to bootstrap the AppModule
in a main.ts
file like this one:
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';import { AppModule } from './app/app.module';platformBrowserDynamic().bootstrapModule(AppModule);
Components
A component controls a patch of screen called a view.
You define a component’s application logic–what it does to support the view–inside a class.The class interacts with the view through an API of properties an methods.
export class HeroListComponent implements OnInit { heroes: Hero[]; selectedHero: Hero; constructor(private service: HeroService) { } ngOnInit() { this.heroes = this.service.getHeroes(); } selectHero(hero: Hero) { this.selectedHero = hero; }}
Angular creates,updates,and destroys components as the user moves through the application.Your app can take action at each moment in this lifecycle through optional lifecycle hooks,like ngOnInit() declared above.
Templates
You define a component’s view with its companion template.A template is a form of HTML that tells Angular how to render the component.
Example:
<h2>Hero List</h2><p><i>Pick a hero from the list</i></p><ul> <li *ngFor="let hero of heroes" (click)="selectHero(hero)"> {{hero.name}} </li></ul><hero-detail *ngIf="selectedHero" [hero]="selectedHero"></hero-detail>
Although this template uses typical HTML elements like <h2>
and <p>
,it also has some differences.Code like *ngFor,{{hero.name}},(click),[hero],and <hero-detail>
uses Angular’s template syntax.
In the last line of the template,the <hero-detail>
tag is a custom element that represents a new component,HeroDetailComponent.
Metadata
Metadata tells Angular how to process a class.
In fact, Component is just a class. it’s not a component until you tell Angular about it.
To tell Angular that Component is a component,attach metadata to the class.
In TypeScript,you attach metadata by using a decorator.Example:
@Component({ selector: 'hero-list', templateUrl: './hero-list.component.html', providers: [ HeroService ]})export class HeroListComponent implements OnInit {/* . . . */}
Here is the @Component decorator,which identifies the class immediately below as a component class.
The @Component decorator takes a required configuration object with the information Angular needs to create and present the component and its view.
Here are a few of the most useful *@Component * configuration options:
- selector: CSS selector.
- templateUrl: module-relative address of this component’s HTML template.
- providers:array of dependency injection providers for services that the component requires.This is one way to tell Angular that the component’s constructor requires a HeroService so it can get the list of the heroes to display.
The metadata in the @component tells Angular where to get the major building blocks you specify for the component.
The template,metadata,and component together describe a view.
- Angular --基本构造
- Angular --基本构造(二)
- angular的基本指令
- angular基本应用
- angular之$location基本用法
- angular-route基本使用Demo
- 揭秘Angular 2-基本类型
- 循环队列基本构造
- 栈的基本构造
- UML基本构造块
- hadoop的基本构造
- 类的基本构造
- 构造方法基本理解
- Angular 2.0项目结构基本介绍
- angular 2.0基本语法总结(一)
- Angular开发(十八)-路由的基本认识
- Angular中app-directive的基本构成
- Angular 2 应用的 8个主要构造块
- 关于android中如何刷新ui
- 有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
- 请求地址映射(@RequestMapping)
- python 用户登陆
- leetcode[Binary Tree Paths]//待整理多种解法
- Angular --基本构造
- 安卓经纬度转换
- 浅谈SVD的理解
- Python学习4-流程控制
- BZOJ 2654: tree (MST) 题解
- spring boot log4j2配置(使用log4j2.yml文件)
- JUnit基本用法
- C++日期计算器的实现
- Java访问者模式