Skip to content

Commit 43436d4

Browse files
committed
refactor: 代码内容优化
1 parent 72e1f93 commit 43436d4

15 files changed

+49
-100
lines changed

Diff for: src/app/app.module.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import {AppComponent} from "./app.component";
99

1010
import {CollectionComponent} from './collection/collection.component';
1111
import {ListComponent, ItemComponent} from './list';
12-
import {DetailComponent, AnotationComponent} from './detail';
12+
import {DetailComponent} from './detail';
1313
import {EditComponent} from './edit/edit.component';
1414

1515
import {ContactService} from "./shared/contact.service";
1616
import {FooterComponent} from './shared/footer.component';
17-
import {DateReformPipe} from './shared/date-reform.pipe';
17+
import {PhonePipe} from './shared/phone.pipe';
1818
import {BtnClickDirective} from './shared/btn-click.directive';
1919
import {UtilService} from './shared/util.service';
2020

@@ -24,12 +24,11 @@ import {UtilService} from './shared/util.service';
2424
AppComponent,
2525
ListComponent,
2626
DetailComponent,
27-
AnotationComponent,
2827
CollectionComponent,
2928
EditComponent,
3029
FooterComponent,
3130
ItemComponent,
32-
DateReformPipe,
31+
PhonePipe,
3332
BtnClickDirective
3433
],
3534
imports : [BrowserModule, FormsModule, HttpModule, RouterModule.forRoot(rootRouterConfig)],

Diff for: src/app/app.routes.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,7 @@ export const rootRouterConfig: Routes = [
1818
},
1919
{
2020
path: "list/:id",
21-
component: DetailComponent,
22-
children: [
23-
{
24-
path: "",
25-
component: AnotationComponent
26-
}
27-
]
21+
component: DetailComponent
2822
},
2923
{
3024
path: "edit",

Diff for: src/app/collection/collection.component.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ <h3 *ngIf="coll_length == 0" class="no-collection">没有收藏</h3>
33
<ul class="my-collection">
44
<li *ngFor="let collection of collections">
55
<a [routerLink]="['/list', collection.id]">
6-
<label>{{ collection.name }}</label><span>{{ collection.telNum }}</span>
6+
<label>{{ collection.name }}</label><span>{{ collection.telNum | phone }}</span>
77
</a>
88
</li>
99
</ul>

Diff for: src/app/detail/anotation.component.css

-7
This file was deleted.

Diff for: src/app/detail/anotation.component.html

-4
This file was deleted.

Diff for: src/app/detail/anotation.component.ts

-11
This file was deleted.

Diff for: src/app/detail/detail.component.css

+8
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,11 @@
8080
line-height: 20px;
8181
padding-top: 5px;
8282
}
83+
84+
.intro{
85+
padding: 20px 15px 0;
86+
}
87+
.intro h3{
88+
font-size: 18px;
89+
padding-bottom: 10px;
90+
}

Diff for: src/app/detail/detail.component.html

+6-3
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,23 @@
1616
<ul class="detail-info">
1717
<li>
1818
<p>手机号码:</p>
19-
<p>{{ detail.telNum }}</p>
19+
<p>{{ detail.telNum | phone }}</p>
2020
</li>
2121
<li>
2222
<p>邮箱:</p>
2323
<p>{{ detail.email }}</p>
2424
</li>
2525
<li>
2626
<p>生日:</p>
27-
<p>{{ detail.birthday | dateReform:'yyyy-MM-dd'}}</p>
27+
<p>{{ detail.birthday | date:'yyyy-MM-dd'}}</p>
2828
</li>
2929
<li>
3030
<p>住址:</p>
3131
<p>{{ detail.address }}</p>
3232
</li>
3333
</ul>
34-
<router-outlet></router-outlet>
34+
<article class="intro">
35+
<h3>简介:</h3>
36+
生活态度好,热爱运动
37+
</article>
3538
</div>

Diff for: src/app/detail/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
export * from './detail.component';
2-
export * from './anotation.component';

Diff for: src/app/edit/edit.component.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,15 @@ export class EditComponent implements OnInit {
123123
}
124124
onBlurPhone() {
125125
this.phoneTip = true;
126-
this.isPhoneNum = util.checkPhoneNum(this.contact.telNum);
126+
this.isPhoneNum = this.util.checkPhoneNum(this.contact.telNum);
127127
}
128128
onBlurAddr() {
129129
this.addrTip = true;
130130
this.isAddr = this.contact.address ? true : false;
131131
}
132132
onBlurEmail() {
133133
this.emailTie = true;
134-
this.isEmail = util.checkEmail(this.contact.email);
134+
this.isEmail = this.util.checkEmail(this.contact.email);
135135
}
136136
onBlurBir() {
137137
this.birTip = true;

Diff for: src/app/list/item.component.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<img class="icon-head" src="./images/Iverson.jpg">
33
<div class="contact-info">
44
<label class="contact-name">{{ contact.name }}</label>
5-
<span class="contact-tel">{{ contact.telNum }}</span>
5+
<span class="contact-tel">{{ contact.telNum | phone }}</span>
66
<i class="contact-to-detail"></i>
77
</div>
88
</a>

Diff for: src/app/shared/btn-click.directive.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ export class BtnClickDirective {
1111
constructor(el: ElementRef) {
1212
this.el = el.nativeElement;
1313
}
14-
btnClick() { this._addClasssName("btn-click"); }
14+
btnClick() {
15+
this._addClasssName("btn-click");
16+
}
1517
private _addClasssName(className: string) {
1618
this.el.className = this.el.className + " " + className;
1719
}

Diff for: src/app/shared/contact.service.ts

+8-26
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import {Injectable} from '@angular/core';
22
import {Http, RequestOptions, Headers} from '@angular/http';
3-
import {Observable} from 'rxjs/Rx';
3+
import 'rxjs/add/operator/map';
44

5-
const contact_url = `./app/contacts.json`;
5+
const CONTACT_URL = `./app/contacts.json`;
66

77
@Injectable()
88
export class ContactService {
99
constructor(
10-
private _http:Http
10+
private http:Http
1111
) {}
1212

1313
request(url:string, opts:any) {
14-
return this._http.request(url, new RequestOptions(opts))
14+
return this.http.request(url, new RequestOptions(opts))
1515
.map(res => {
1616
let _res = res.json();
1717
if(opts.id){
@@ -41,41 +41,23 @@ export class ContactService {
4141
}
4242

4343
getContactsData() {
44-
return new Observable(observable => {
45-
this.get(contact_url)
46-
.subscribe(res => {
47-
observable.next(res);
48-
observable.complete();
49-
})
50-
})
44+
return this.get(CONTACT_URL);
5145
}
5246

5347
getContactById(id:number) {
54-
return new Observable(observable => {
55-
this.get(contact_url, {id:id})
56-
.subscribe(res => {
57-
observable.next(res);
58-
observable.complete();
59-
})
60-
})
48+
return this.get(CONTACT_URL, { id: id })
6149
}
6250

6351
getCollections() {
64-
return new Observable(observable => {
65-
this.get(contact_url, {collection:1})
66-
.subscribe(res => {
67-
observable.next(res);
68-
observable.complete();
69-
})
70-
})
52+
return this.get(CONTACT_URL, { collection: 1 });
7153
}
7254

7355
addContact(obj:Object = {}) {
7456
let body = JSON.stringify(obj);
7557
let headers = new Headers({ 'Content-Type': 'application/json' });
7658
let options = new RequestOptions({ headers: headers });
7759

78-
return this._http.post(contact_url, body, options).map(res => res.json());
60+
return this.http.post(CONTACT_URL, body, options).map(res => res.json());
7961
}
8062

8163
}

Diff for: src/app/shared/date-reform.pipe.ts

-32
This file was deleted.

Diff for: src/app/shared/phone.pipe.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Pipe, PipeTransform } from '@angular/core';
2+
3+
@Pipe({
4+
name: 'phone'
5+
})
6+
export class PhonePipe implements PipeTransform {
7+
transform(val:string):string {
8+
if(!val) return '';
9+
if(val.length === 11) {
10+
return val.replace(/(\d{3})(\d{4})(\d{4})/, (m, m1, m2, m3) => {
11+
return [m1, m2, m3].join('-');
12+
})
13+
}
14+
return val;
15+
}
16+
}

0 commit comments

Comments
 (0)