Angular五大特点

https://angular.io/guide/what-is-angular
Angular applicaitons: The essentials
This section explain the core ideas behind Angular.
Understanding these ideas can help you design and build your applicaitons more effectively.
1.Components -- 组件
Components are the building blocks that compose an application.
A component includes a TypeScript class with a @Component() decorator, an HTML template, and styles.
The @Component() decorator specifies the following Angular-specific information:
- A CSS selector that defines how the component is used in a template. HTML elementss in your
  template that match this selector become instances of the component.
- An HTML template that instructs Angular how to render the component.
- An optional set of CSS styles that define the appearance of the template's HTML elements.  

The following is a minimal Angular component.
----------------------------------------------------
import { Component } from '@angular/core';

@Component({
  selector: 'hello-world',
  template: `
    <h2>Hello World</h2>
    <p>This is my first component!</p>
    `,
})
export class HelloWorldComponent {
  // The code in this class drives the component's behavior.
}
----------------------------------------------------
To use this component, you write the following in a template:
----------------------------------------------------
<hello-world></hello-world>
----------------------------------------------------
When Angular renders this component, the resulting DOM looks like this:
----------------------------------------------------
<hello-world>
  <h2>Hello World</h2>
  <p>This is my first component!</p>
</hello-world>  

----------------------------------------------------
Angular's component model offers strong encapsulation and an intuitive application structure.
Components also make your application easier to unit test and can improve the overall readability of your code.
For more information on what you can do with components, see the Components section.

2. Templates 模板
Every component has an HTML template that declares how that component renders.
You define this template either inline or by file path.

Angular extends HTML with addtional syntax that lets you insert dynamic values from your component.
Angular automatically updates the rendered DOM when your component's state changes.
One application of this feature is inserting dynamic text, as shown in the flowing example.
----------------------------------------------------
<p>{{ message }}</p>
----------------------------------------------------
The value for message comes from the component class:
----------------------------------------------------
import { Component } from '@angular/core';

@Component ({
  selector: 'hello-world-interpolation',
  templateUrl: './hello-world-interpolation.component.html'
})
export class HelloWorldInterpolationComponent {
  message = 'Hello, World!';
}
----------------------------------------------------
When the application loads the component and its template, the user sees the following:
<p>Hello, World!</p>
Notice the use of double curly braces -- they instuct Angular to interpolate the contents within them.
Angular also supports property bindings, to help you set values for properties and  attributes of HTML elements and pass values to your application's presentation logic.
----------------------------------------------------
<p [id]="sayHelloId" [style.color]="fontColor">You can set my color in the component!</p>
----------------------------------------------------
Notice the use of the square brackets方括号--that syntax indicates that you're binding the property or attribute to a value in the component class.

You can also declare event listeners to listen for and respond to user actions such as keystrokes,mouse movements, clicks, and touches.
You declare an event listener by specifying the event name in parentheses: 圆括号
----------------------------------------------------
<button (click)="sayMessage()" [disabled]="canClick">Trigger alert message</button>
----------------------------------------------------
The preceding example calls a method, which is defined in the component class:
----------------------------------------------------
sayMessage() {
  alert(this.message);
}
----------------------------------------------------
The following is an example of interpolation and bindings within an Angular template:
hello-world-bindings.component.ts-------------------
import { Component } from '@angular/core';
@Component ({
  selector: 'hello-world-bindings',
  templateUrl: './hello-world-bindings.component.html'
})
export class HelloWorldBindingsComponent{
  fontColor = 'blue';
  sayHelloId = 1;
  canClick = false;
  message = 'Hello, World';
  sayMessage() {
    alert(this.message);
  }
}
hello-world-bindings.component.ts-------------------
hello-world-bindings.component.html-------------------
<button (click)="sayMessage()" [disabled]="canClick">Trigger alert message</button>
<p [id]="sayHelloId" [style.color]="fontColor">You can set my color in the component!</p>
hello-world-bindings.component.html-------------------

You can add additional functionality to your templates through the use of directives.
The most popular directives in Angular are *ngIf and *ngFor.
You can use directives to perform a variety of tasks, such as dynamically modifying the DOM structure.
And you can also create your own custom directives to create great user experiences.

The following code is an example of the *ngIf directive.
hello-world-ngif.component.ts-------------------
import { Component } from '@angular/core';

@Component({
  selector: 'hello-world-ngif',
  templateUrl: './hello-world-ngif.component.html'
})
export class HelloWorldNgIfComponent {
  message = 'I/' m read only!';
  canEdit = false;
  
  onEditClick() {
    this.canEdit = !this.canEdit;
    if(this.canEdit) {
      this.message = 'You can edit me!';
    } else {
      this.message = 'I \' m read only!';
    }
  }
}


hello-world-ngif.component.ts-------------------
hello-world-ngif.component.html-------------------

<h2>Hello World: ngIf! </h2>
<button (click)="onEditClick()">Make text editable!</button>
<div *ngIf="canEdit; else noEidt">
  <p>You can edit the following paragraph.</p>
</div>
<ng-template #noEidt>
  <p>The following paragraph is read only. Try clicking the button!</p>
<ng-template>
<p [contentEditabl]="canEdit">{{ message }}</p>

hello-world-ngif.component.html-------------------

Angular's declarative templates allow you to cleanly separate your application's logic from its presentation. 
Templates are based on standard HTML, so they're easy to build, maintain, and update.
For more information on what you can do with teplates, see the Templates section.

3. Dependency injection
Dependency injection allow you to declare the dependencies of your TypeScript classes without taking care of their instantiation. Instead, Angular handles the instantiation for you.

This design pattern allows you to write more testable and flexible code.
Even though understanding dependency injection is not critical to start using Angular, we strongly recommend it as a best practice and many aspects of Angular take adavantage of it to some degree.

To illustrate how dependency injection works, consider the following example.
The first file, logger.service.ts, define a Logger class. This class contains a writeCount function that logs a number to the console.
---------------------------
import { Injectalbe } from '@angular/core '

@Injectable({providedIn: 'root'})
export class Logger {
  writeCount(count: number) {
    console.warn(count);
  }
}
---------------------------
Next, the hello-world-di.component.ts file defines an Angular component.
This component contains a button that uses the writeCount function of the Logger class.
To access that function, the Logger service is injected into the HelloWorldDI class by adding private logger: Logger to the constructor.
---------------------------
import { Component } from '@angular/core';
import { Logger } from '../logger.service';

@Component ({
  selector: 'hello-world-di',
  templateUrl: './hello-world-di.component.html'
})
export class HelloWorldDependencyInjectionComponent {
  count = 0;
  constructor(private logger: Logger) {  //构造注入法
  
  }
  
  onLogMe() {
    this.logger.writeCount(this.count);
    this.count++;
  }
}


---------------------------
For more information about dependency injection and Angular, see the Dependency injection in Angular section.

4. Angular CLI
The Angular CLI is the fastest, easiest, and recommended way to develop Angular applications.
The Angular CLI makes a number of tasks easy. Here are some examples:
-ng build: Compiles an Angular app into an output directory.
-ng serve: Builds and serves your application,rebuilding on file changes.
-ng generate: Generates or modifies files based on a schematic.
-ng test: Runs unit tests on a given project.
-ng e2e: Builds and serves an Angular application, then runs end-to-end tests.

You'll find the Angular CLI a valuable tool for building out your applications.
For more information about the Angular CLI, see the CLI Reference section.

5. Firt-party libraries
The section, Angular applications: The essentials,provides a brief overview of a couple of the key architectural elements you'll use when building Angular applications.
But the many benefits of Angular really become apparent when your application grows and you want to add additional fucntions such as site navigation or user input.
That's when you can leverage the Angular platform to incorporate one of the many firt-party libraries that Angular provides.

Some of the libraries available to you include:
-Angular Router: Advanced client-side navigation and routing based on Angular components.
  Supports lazy-loading, nested routes, custom path matching, and more.
-Angular Forms: Uniform system for form participation and validation.
-Angular HttpClient: Robust HTTP client that can power more advanced client-server communication.
-Angular Animations: Rich system for driving animations based on application state.
-Angular PWA: Tools for building Progressive Web Application(PWAs) including a service worker Web app manifest.
-Angular Schematics: Automated caffolding, refactoring, and update tools that simplify development at large scale.
  
These libraries expand your application's functionality while also allowing you to focus more on the features that make your application unique.
And you can add these libraries knowing that they're designed to integarte seamlessly into and update simultaneously with the Angular framework.

These libraries are only required if and when they can help you add functionality to your applications or solve a particular problem.

03-14
### Angular 框架概述 Angular 是一个由 Google 维护的开源前端开发框架,旨在帮助开发者构建动态单页应用程序 (SPA)[^2]。该框架自 2016 年推出 Angular 2 后进行了全面重构,并采用了基于组件的设计模式,显著提高了性能和可维护性。 #### 历史沿革 - **AngularJS(Angular 1.x)**: 首次发布于 2010 年,其核心特性包括 MVC 架构、双向数据绑定以及依赖注入机制[^1]。然而,在处理复杂项目时存在明显的性能瓶颈和扩展困难等问题。 - **Angular 2+**: 自 2016 年起发布的版本统称为 Angular,引入了许多现代化功能和技术栈支持,例如 TypeScript 编程语言作为主要实现工具之一[^3]。此后每隔约六个月便会更新一次主版本号直到目前最新的第十五代产品问世为止。 #### 技术亮点 - 使用 **TypeScript** 进行编码可以享受更强类型的静态检查优势从而减少运行期错误发生概率的同时也使得代码更加结构化易于理解维护; - 支持模块化的应用设计思路通过 `app.module.ts` 文件定义根级 NgModule 来管理整个项目的各个部分之间的关系配置等信息; - 提供强大的指令系统允许用户创建自定义 HTML 标签来增强页面交互能力; - 内置服务端通信解决方案 HttpClient 可方便快捷地发起 RESTful API 请求获取远程资源数据并对其进行序列化反序列化操作以便后续业务逻辑处理需求得到满足。 以下是关于如何初始化一个新的 Angular 应用程序实例演示: ```bash npm install -g @angular/cli ng new my-app cd my-app ng serve --open ``` 上述命令会利用官方提供的 CLI 工具链快速搭建好基础环境设置完成后启动本地服务器监听指定端口等待访问测试效果展示出来给到我们查看验证正确与否情况之下继续深入探索更多高级特性和最佳实践方法论等内容领域范畴之内不断积累经验提升技能水平达到预期目标成就自我价值体现最大化程度之上即可成功完成既定任务指标要求标准范围内合理范围之内的成果产出表现形式呈现方式多样化多维度全方位立体式展现个人风采魅力所在之处彰显独特个性特征区别与其他竞争对手的优势地位确立稳固根基奠定坚实基础保障长远可持续健康发展态势良好局面形成良性循环生态体系建立完善健全规章制度流程规范化标准化统一协调一致行动步调整齐划一共同前进方向明确清晰可见未来前景光明灿烂辉煌无比美好幸福生活向往追求梦想成真愿望达成圆满结局收场完美谢幕时刻到来之际让我们一起见证这一伟大历史性瞬间永恒铭记心中永不磨灭记忆深处珍藏永远怀念珍惜当下把握现在展望明天充满信心希望满怀憧憬期待无限可能创造奇迹改变世界影响深远意义非凡贡献巨大功不可没值得骄傲自豪荣耀加身光彩照人形象树立典范榜样力量无穷激励鼓舞人心士气高涨斗志昂扬奋勇向前无坚不摧所向披靡战无不胜攻无不克百战百胜常胜将军威名远播名垂青史流芳千古万世敬仰传颂歌咏赞美不已赞叹连连掌声雷动欢呼雀跃欢声笑语洋溢满堂喜庆气氛浓厚热烈庆祝活动丰富多彩精彩纷呈令人目不暇接眼花缭乱应接不暇叹为观止拍手叫绝交口称赞好评如潮口碑载道信誉卓著信赖可靠安全放心省心安心舒心愉悦心情舒畅身体康健长寿百年千秋永固基业长存繁荣昌盛国泰民安天下太平盛世祥瑞降临福泽绵延子孙后代繁衍兴旺发达蒸蒸日上欣欣向荣蓬勃发展蒸蒸日上再创佳绩续写传奇谱写新华章开启新征程迈向新高度攀登新高峰铸就新辉煌!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值