ng generate component hero-detail
创建目录 src/app/hero-detail。
在这个目录中会生成四个文件:
该命令还会把 HeroDetailComponent 添加到 src/app/app.module.ts 文件中 @NgModule 的 declarations 列表中。
click 外面的圆括号会让 Angular 监听这个元素的 click 事件。
添加 click 事件处理器
selectedHero: Hero;
onSelect(hero: Hero): void {
this.selectedHero = hero;
}
使用 *ngIf 隐藏空白的详情
使用ngModel
[(ngModel)] 是 Angular 的双向数据绑定语法。
虽然 ngModel 是一个有效的 Angular 指令,不过它在默认情况下是不可用的。
它属于一个可选模块 FormsModule,你必须自行添加此模块才能使用该指令。
有些元数据位于 @Component 装饰器中,你会把它加到组件类上。 另一些关键性的元数据位于 @NgModule 装饰器中。
最重要的 @NgModule 装饰器位于顶级类 AppModule 上。
Angular CLI 在创建项目的时候就在 src/app/app.module.ts 中生成了一个 AppModule 类。 这里也就是你要添加 FormsModule 的地方。
导入 FormsModule
打开 AppModule (app.module.ts) 并从 @angular/forms 库中导入 FormsModule 符号。
import { FormsModule } from '@angular/forms';
app.module.ts文件
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms'//插入的FormModule
import { AppComponent } from './app.component';
import { HeroesComponent } from './heroes/heroes.component';
@NgModule({
declarations: [
AppComponent,
HeroesComponent
],
imports: [
BrowserModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
ng generate service hero
按照惯例,这个模块类的名字叫做 AppRoutingModule,并且位于 src/app 下的 app-routing.module.ts 文件中。
ng generate module app-routing --flat --module=app
–flat 把这个文件放进了 src/app 中,而不是单独的目录中。
–module=app 告诉 CLI 把它注册到 AppModule 的 imports 数组中。
npm install angular-in-memory-web-api --save
导入 HttpClientInMemoryWebApiModule 和 InMemoryDataService 类,在/app.module.ts
this.itFormGroup = this.fb.group({
lxnameCtr: ["", [Validators.required]],
remarkCtr: [""]
});
createForm() {
this.heroForm = this.fb. group({
name: ['', Validators.required ],
street: '',
city: '',
state: '',
zip: '',
power: '',
sidekick: ''
});
}
FormBuilder.group 是一个用来创建 FormGroup 的工厂方法,它接受一个对象,对象的键和值分别是
FormControl 的名字和它的定义
更多的可以参考网址:https://segmentfault.com/a/1190000010064866
②响应式表单的更新值 setValue、patchValue
创建响应式表单是需要在app.module.ts中导入ReactiveFormsModule模块
this.heroForm.patchValue({
name: this.hero.name
});
this.heroForm.setValue({
name: this.hero.name,
address: this.hero.addresses[0] || new Address()
});
判断移动端设备,区分android,iphone,ipad和其它
var ua = navigator.userAgent.toLowerCase();
if(ua.match(/android/i)) == "android") {
alert("android");
}
if(ua.match(/iPhone/i)) == "iPhone") {
alert("iPhone");
}
if(ua.match(/iPad/i)) == "iPad") {
alert("iPad");
}
var ua = navigator.userAgent.toLowerCase();
if(ua.match(/weibo/i) == "weibo") {
return 1;
} else if(ua.indexOf('qq/') != -1) {
return 2;
} else if(ua.match(/MicroMessenger/i) == "micromessenger") {
var v_weixin = ua.split('micromessenger')[1];
v_weixin = v_weixin.substring(1, 6);
v_weixin = v_weixin.split(' ')[0];
if(v_weixin.split('.').length == 2) {
v_weixin = v_weixin + '.0';
}
if(v_weixin < '6.0.2') {
return 3;
} else {
return 4;
}
} else {
return 0;
}
var sUserAgent = navigator.userAgent.toLowerCase();
var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
var bIsMidp = sUserAgent.match(/midp/i) == "midp";
var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
var bIsAndroid = sUserAgent.match(/android/i) == "android";
var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
if(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
alert("您是手机登录");
} else {
alert("您是电脑登录");
}
function isIE(){
var userAgent = navigator.userAgent,
rMsie = /(msie\s|trident.*rv:)([\w.]+)/,
rFirefox = /(firefox)\/([\w.]+)/,
rOpera = /(opera).+version\/([\w.]+)/,
rChrome = /(chrome)\/([\w.]+)/,
rSafari = /version\/([\w.]+).*(safari)/;
var browser;
var version;
var ua = userAgent.toLowerCase();
var match = rMsie.exec(ua);
if (match != null) {
ieVersion = { browser : "IE", version : match[2] || "0" };
return true;
}
var match = rFirefox.exec(ua);
if (match != null) {
var ffVersion = { browser : match[1] || "", version : match[2] || "0" };
return false;
}
var match = rOpera.exec(ua);
if (match != null) {
var opVersion = { browser : match[1] || "", version : match[2] || "0" };
return false;
}
var match = rChrome.exec(ua);
if (match != null) {
var chVersion = { browser : match[1] || "", version : match[2] || "0" };
return false;
}
var match = rSafari.exec(ua);
if (match != null) {
var sfVersion = { browser : match[2] || "", version : match[1] || "0" };
return false;
}
if (match != null) {
var ohterVersion = { browser : "", version : "0" };
return false;
}
}
import { Component, EventEmitter, Input, OnDestroy, OnInit, Output } from "@angular/core";
import { MyhttpService } from "../myhttp.service";
declare var $: any;
@Component({
template: `
提示信息
`,
styles: [
".toast-title{font-weight:bold;font-size:16px;}",
".toast-message{-ms-word-wrap:break-word;word-wrap:break-word}",
".toast-message a,.toast-message label{color:#fff}",
".toast-message a:hover{color:#ccc;text-decoration:none}",
".toast-close-button{position:relative;right:-0.3em;top:-0.3em;float:right;font-size:20px;font-weight:bold;color:#fff;-webkit-text-shadow:0 1px 0 #fff;text-shadow:0 1px 0 #fff;opacity:.8;-ms-filter:alpha(opacity=80);filter:alpha(opacity=80)}.toast-close-button:hover,.toast-close-button:focus{color:#000;text-decoration:none;cursor:pointer;opacity:.4;-ms-filter:alpha(opacity=40);filter:alpha(opacity=40)}button.toast-close-button{padding:0;cursor:pointer;background:transparent;border:0;-webkit-appearance:none}.toast-top-center{top:0;right:0;width:100%}",
"#toast-container{position:fixed;z-index:999999;right:12px;bottom:-150px;}",
"#toast-container>div{margin:0 0 6px;padding:15px 15px 15px 50px;width:300px;-moz-border-radius:3px 3px 3px 3px;-webkit-border-radius:3px 3px 3px 3px;border-radius:3px 3px 3px 3px;background-position:15px center;background-repeat:no-repeat;-moz-box-shadow:0 0 12px #999;-webkit-box-shadow:0 0 12px #999;box-shadow:0 0 12px #999;color:#fff;opacity:.8;-ms-filter:alpha(opacity=80);filter:alpha(opacity=80)}",
],
providers: [MyhttpService]
})
export class GgmsgComponent implements OnInit, OnDestroy {
@Input() data: any
@Output() outer: EventEmitter = new EventEmitter();
ngOnInit() {
}
ngOnDestroy() {
}
clsoe(): void {
this.outer.emit({
type: "closeggmsg" //约定一个,这里告诉父组件要关闭了
});
}
}
@Input():父组件将数据传过来,子组件接收
@Output():
我这里是dynamaner.component.ts
import {GgmsgComponent} from "./ggMsg.component";
const CompMap = {
'ggmsg':GgmsgComponent,
}
const DynaCompArr = [
GgmsgComponent
]
export {
CompMap,
DynaCompArr,
}
在 Angular 中放置组件的地方称为 container 容器。这里我放置在app.component组件中,为了方便以后每个页面都显示
import {Component, ViewContainerRef, ViewChild, ComponentFactoryResolver} from '@angular/core';
import {NavigationEnd, NavigationError, NavigationStart, Router} from "@angular/router";
import {CompMap} from "./dynacomponent/dynamaner.component"; //将存放动态组件的文件引进来
export class AppComponent {
@ViewChild("pubMsgRoom", {read: ViewContainerRef}) rRoom: ViewContainerRef;
}
this.rRoom.remove(0); //清空组件(有没有都先清空)每次我们需要创建组件时,我们需要删除之前的视图,否则组件容器中会出现多个视图
const ggmsgcomponent : ComponentFactory= this.cfr.resolveComponentFactory(CompMap['ggmsg']); //**resolveComponentFactory()** 方法接受一个组件并返回如何创建组件的 **ComponentFactory** 实例。
const ggmsgcomp : ComponentRef = this.rRoom.createComponent(ggmsgcomponent); //我们调用容器的 **createComponent()** 方法,该方法内部将调用 **ComponentFactory** 实例的 **create()** 方法创建对应的组件,并将组件添加到我们的容器
ggmsgcomp.instance.data = {}; //我们已经能获取新组件的引用,即可以我们可以设置组件的输入类型
ggmsgcomp.instance.outer.subscribe((resp) => {} //同样我们也可以订阅组件的输出属性
ggMsg.component.ts
import { Component, EventEmitter, Input, OnDestroy, OnInit, Output } from "@angular/core";
import { MyhttpService } from "../myhttp.service";
declare var $: any;
@Component({
template: `
提示信息
`,
styles: [
".toast-title{font-weight:bold;font-size:16px;}",
".toast-message{-ms-word-wrap:break-word;word-wrap:break-word}",
".toast-message a,.toast-message label{color:#fff}",
".toast-message a:hover{color:#ccc;text-decoration:none}",
".toast-close-button{position:relative;right:-0.3em;top:-0.3em;float:right;font-size:20px;font-weight:bold;color:#fff;-webkit-text-shadow:0 1px 0 #fff;text-shadow:0 1px 0 #fff;opacity:.8;-ms-filter:alpha(opacity=80);filter:alpha(opacity=80)}.toast-close-button:hover,.toast-close-button:focus{color:#000;text-decoration:none;cursor:pointer;opacity:.4;-ms-filter:alpha(opacity=40);filter:alpha(opacity=40)}button.toast-close-button{padding:0;cursor:pointer;background:transparent;border:0;-webkit-appearance:none}.toast-top-center{top:0;right:0;width:100%}",
"#toast-container{position:fixed;z-index:999999;right:12px;bottom:-150px;}",
"#toast-container>div{margin:0 0 6px;padding:15px 15px 15px 50px;width:300px;-moz-border-radius:3px 3px 3px 3px;-webkit-border-radius:3px 3px 3px 3px;border-radius:3px 3px 3px 3px;background-position:15px center;background-repeat:no-repeat;-moz-box-shadow:0 0 12px #999;-webkit-box-shadow:0 0 12px #999;box-shadow:0 0 12px #999;color:#fff;opacity:.8;-ms-filter:alpha(opacity=80);filter:alpha(opacity=80)}",
],
providers: [MyhttpService]
})
export class GgmsgComponent implements OnInit, OnDestroy {
loginuseruuid: string = "";
//已读未读
wdcount: string = "--";
ydcount: string = "--";
tmid: any;
@Input() data: any
@Output() outer: EventEmitter = new EventEmitter();
constructor(private itservice: MyhttpService) {
}
ngOnInit() {
clearTimeout(this.tmid);
this.getLogmsg();
}
ngOnDestroy() {
clearTimeout(this.tmid);
}
clsoe(): void {
this.outer.emit({
type: "closeggmsg"
});
}
getLogmsg(): void {
this.itservice.requstUrl("security_client/auth/getLoginUser", null).subscribe(resp => {
const retflag = resp.result;
if (retflag == "success") {
this.loginuseruuid = resp.rows[0].user_uuid;
this.getMsg();
}
});
}
getMsg() {
this.itservice.requstUrl("owner_client/owner/OaGwtz/JsrUuidByGwtzZtCount", {
jsruuid: this.loginuseruuid,
randomv: Math.random(),
}).subscribe(resp => {
const retflag = resp.result;
if (retflag == "success") {
$("#toast-container").stop();
$("#toast-container").animate({
bottom: '10px'
}, "slow");
const rowobj = resp.rows;
this.wdcount = rowobj.wck;
this.ydcount = rowobj.yck;
clearTimeout(this.tmid)
this.tmid = setTimeout(function () {
$("#toast-container").fadeOut()
}, 5000);
}
});
}
}
app.component.ts
import {Component, ViewContainerRef, ViewChild, ComponentFactoryResolver} from '@angular/core';
import {NavigationEnd, NavigationError, NavigationStart, Router} from "@angular/router";
import {LocalStorageService} from "./local-storage-service";
import {CommService} from "./comm-service";
import {CompMap} from "./dynacomponent/dynamaner.component";
declare var $: any;
@Component({
selector: 'app-root',
template: `
`,
styles: [
".page-spinner-bar>div{margin: 0 5px;width: 18px;height: 18px;background: orange;border-radius: 100% !important;display: inline-block;-webkit-animation: bounceDelay 1.4s infinite ease-in-out;animation: bounceDelay 1.4s infinite ease-in-out;-webkit-animation-fill-mode: both;animation-fill-mode: both;}",
".page-spinner-bar .bounce1{-webkit-animation-delay: -0.32s;animation-delay: -0.32s;}",
".page-spinner-bar .bounce2{-webkit-animation-delay: -0.16s;animation-delay: -0.16s;}",
".main_out{position: relative}",
'#myMask{display: none;position:absolute;left: 0px;top: 0px;z-index: 1000;background-color: grey;opacity: .1}',
".pubMsg{position:absolute;right:5px;bottom:5px;width:auto;height:auto}",
],
})
export class AppComponent {
hasanima: boolean = false;
setTm: any;
@ViewChild("pubMsgRoom", {read: ViewContainerRef}) rRoom: ViewContainerRef;
constructor(private router: Router,
private localstorage: LocalStorageService,
private commservice: CommService,
private cfr: ComponentFactoryResolver) {
this.router.events.subscribe(event => {
//路由开始切换
if (event instanceof NavigationStart) {
this.hasanima = true;
//显示三个点
const token = this.commservice.getToken();
const localtoken = this.localstorage.get("token");
const url = event.url;
if (!token) { //刷新操作 或者 第一次进入系统登入页面
if (url == "/" || url == "/login") {
//第一次进入系统登入页面不处理
} else {
if (!localtoken) {
//清空缓存后刷新操作
this.router.navigate(["login"]);
} else {
//简单的刷新操作
this.commservice.setToken(localtoken);
clearInterval(this.setTm);
this.setTm = setTimeout(() => {
this.createComponent(url);
}, 2000)
}
}
} else {
this.createComponent(url);
}
}
//路由切换结束
if (event instanceof NavigationEnd) {
//隐藏三个点
this.hasanima = false;
}
//路由切换出错
if (event instanceof NavigationError) {
this.hasanima = false;
console.log('路由切换出错');
}
});
}
createComponent(url: any) {
// 公告的提示信息
if (url == "/taskpanelM/workcenter/mygg") {
this.rRoom.remove(0);
const ggmsgcomponent = this.cfr.resolveComponentFactory(CompMap['ggmsg']);
const ggmsgcomp = this.rRoom.createComponent(ggmsgcomponent);
ggmsgcomp.instance.data = {};
ggmsgcomp.instance.outer.subscribe((resp) => {
const typev = resp.type;
if (typev == "closeggmsg") {
this.rRoom.remove(0);
}
});
} else {
this.rRoom.remove(0);
}
}
}