【Angular】(学习二)项目启动过程分析

1.Angular项目入口文件

1)angular.json:NG项目配置
          index:./src/index.html    <app-root></app-root>
          main:./src/main.ts

(2)main.ts  > bootstrapModule(AppModule)

(3)app.module.ts > bootstrap:[AppComponent]

(4)app.cpmponent.ts > selector:'app-root'
                                           > templateUrl:'app.component.html'

(5)app.component.html > HTML片段

2.Angular核心概念之一:模块

    Module:模块是一个抽象的容器,用语对组件进行分组
    整个应用初试时有且只有一个组件:AppModule

3.Angular核心概念之二:组件

    组件:是一段可以反复使用的页面片段,如页头、轮播、手风琴……

    组件(Component) =  模块(Template) + 脚本(Script) + 样式(Style)

    提示:NG中,任何一个组件都必须声明在一个模块中!

    自定义组件的步骤:
   ①创建组件class
   ②在某个模块中注册组件:declaration
   ③使用已经注册过的组件:<app-test></app-test>

练习:创建一个文件夹mytest,①在自定义组件mytest.component.ts中创建class,使用装饰器修饰 ②在主模块中注册③使用该组件

4.Angular核心概念之三:数据绑定

(1)HTML绑定:{ { NG表达式 }}
       
算术运算(+、-、*、/)、比较运算(>、<、=)、逻辑运算(&&、||)、三目运算(a?b:c)、调用函数、创建对象、JSON序列化

(2)属性绑定:
       
形式1:直接在属性上用{ {}}  <p title="{ {msg}}"></p>
        形式2:使用[]做属性绑定  <p [title]="msd"></p>

(3)事件绑定:

<button [title]="'当前购买数:'+count"
        (click)="addGoodCount()">添加数量</button>

(4)指令绑定:

①样式绑定:[ngStyle]

<button [ngStyle]="myStyleObj" (click)="loadMore()">加载更多</button>

②样式绑定:[ngClass]

<button [ngClass]="myClassObj" (click)="loadMore()">加载更多</button>

③选择绑定:*ngIf

<p *ngIf="isStudyUser">此区域仅学习用户可见</p>
<p *ngIf="age > 18; else forChildrenLook">此区域仅家长可见</p>
<ng-template #forChildrenLook><p>此区域仅宝宝可见</p></ng-template>

④循环绑定:*ngFor

<li *ngFor="let item of members">{
  
  {item}}</li>
<li *ngFor="let item of members;let i=index">{
  
  {i}}-{
  
  {item}}</li>
<li *ngFor="let item of members;index as i">{
  
  {i}}-{
  
  {item}}</li>

⑤特殊的选择绑定:[ngSwitch]

<ANY [ngSwitch]="表达式">
  <ANY *ngSwitchCase="值1"></ANY>
  <ANY *ngSwitchCase="值2"></ANY>
  ……
  <ANY *ngSwitchDefault></ANY>
</ANY>

Angular中指令分为三类:

1、组件

2、结构型指令:会影响DOM树结构,必须用*开头,如*ngFor、*ngIf

3、属性型指令:不会影响DOM树结构,只影响元素外观或行为,必须用[]括起来,如[ngStyle]、[ngClass]

(5)双向绑定:

猜你喜欢

转载自blog.csdn.net/xiaoxiong_jiaxin/article/details/119422935