赞
踩
jQuery是JS的一个函数库,而angularJS是Google开源的前端JS结构化框架。
官网:https://angular.cn/
离线手册下载:https://www.php.cn/xiazai/shouce/58
AngularJS的主要作用是构建单页面应用或Web App应用
单页面应用的特点:
Chrom浏览器下载ng-inspector for AngularJS插件,固定到标签页上,打开一个网页点击该插件,如果不是AngularJS写的就会显示:
如果是AngularJS写的就会显示相应的数据。
eg:微信网页版:
npm i -g @angular/cli
输入ng v
就可以验证是否安装成功,出现如下内容说明安装成功:
ng new 项目名称
创建项目(选择路由和css, 在创建过程会执行npm i 的操作)ng serve --open
运行项目运行成功:
app.component.spec.ts是生成的一个测试文件
文件详解:
// angular的根模块文件,告诉angular如何组装应用 // angular的核心模块 import { NgModule } from '@angular/core'; // 浏览器解析模块 import { BrowserModule } from '@angular/platform-browser'; // 根模块 import { AppComponent } from './app.component'; // @NgModule装饰器,接受一个元数据对象,告诉angular如何编译和启动应用 @NgModule({ // 配置当前项目运行的组件 declarations: [ AppComponent ], // 配置当前模块运行依赖的其他模块 imports: [ BrowserModule ], // 配置项目所需服务 providers: [], // 指定应用的根组件,通过引导根模块AppModule来启动应用 bootstrap: [AppComponent] }) // 跟模块无需导出,因为它不需要被其他组件引用 export class AppModule { }
// 引入核心模块中的Component
import { Component } from '@angular/core';
// 组件装饰器
@Component({
selector: 'app-root',//组件名称
templateUrl: './app.component.html',//组件的html
styleUrls: ['./app.component.css']//组件的css
})
export class AppComponent {
title = 'my-anuglar'; //定义组件的属性
constructor(){
// 组件类的构造函数
}
}
ng g component 组件名
创建组件。app.module.ts
.组件的名称
作为标签
在页面进行引入。<app-news></app-news>
<div>
Angular
</div>
$(function(){})
jQuery实现:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> </head> <body> <input type="text" name="" id=""> <p>您输入的内容是:<span></span></p> <script type='text/javascript' src='https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js'></script> <script> $(function (){ $('input').keyup(function (){ var value = this.value $('span').html(value) }) }) </script> </body> </html>
angular实现:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body ng-app>
<input type="text" ng-model="username">
<p>您输入的内容是:<span>{{username}}</span></p>
<script type='text/javascript' src='./angular/angular-1.2.29/angular.js'></script>
</body>
</html>
angular和jQuery相比:没有写js代码,没有操作Dom元素,直接操作的是动态数据。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。