Skip to content

Commit 08cf534

Browse files
docs(angular): ran linter to format changes to angular walk through
1 parent 2660f4c commit 08cf534

5 files changed

Lines changed: 52 additions & 57 deletions

File tree

docs/angular/your-first-app.md

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ import { defineCustomElements } from '@ionic/pwa-elements/loader'; // Added impo
117117
// Call the element loader before the bootstrapModule/bootstrapApplication call
118118
defineCustomElements(window);
119119

120-
platformBrowserDynamic().bootstrapModule(AppModule)
121-
.catch(err => console.log(err));
120+
platformBrowserDynamic()
121+
.bootstrapModule(AppModule)
122+
.catch((err) => console.log(err));
122123
```
123124

124125
That’s it! Now for the fun part - let’s see the app in action.
@@ -144,9 +145,7 @@ Open the photo-gallery app folder in your code editor of choice, then navigate t
144145
```html
145146
<ion-header [translucent]="true">
146147
<ion-toolbar>
147-
<ion-title>
148-
Tab 2
149-
</ion-title>
148+
<ion-title> Tab 2 </ion-title>
150149
</ion-toolbar>
151150
</ion-header>
152151

@@ -172,9 +171,7 @@ We put the visual aspects of our app into `<ion-content>`. In this case, it’s
172171
```html
173172
<ion-header [translucent]="true">
174173
<ion-toolbar>
175-
<ion-title>
176-
Tab 2
177-
</ion-title>
174+
<ion-title> Tab 2 </ion-title>
178175
</ion-toolbar>
179176
</ion-header>
180177

@@ -201,7 +198,6 @@ Next, open `src/app/tabs/tabs.page.html`. Change the label to “Photos” and t
201198

202199
```html
203200
<ion-tabs>
204-
205201
<ion-tab-bar slot="bottom">
206202
<ion-tab-button tab="tab1" href="/tabs/tab1">
207203
<ion-icon aria-hidden="true" name="triangle"></ion-icon>
@@ -220,7 +216,6 @@ Next, open `src/app/tabs/tabs.page.html`. Change the label to “Photos” and t
220216
<ion-label>Tab 3</ion-label>
221217
</ion-tab-button>
222218
</ion-tab-bar>
223-
224219
</ion-tabs>
225220
```
226221

docs/angular/your-first-app/2-taking-photos.md

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,17 @@ import { Filesystem, Directory } from '@capacitor/filesystem';
5353
import { Preferences } from '@capacitor/preferences';
5454

5555
@Injectable({
56-
providedIn: 'root'
56+
providedIn: 'root',
5757
})
5858
export class PhotoService {
59-
60-
constructor() { }
59+
constructor() {}
6160

6261
public async addNewToGallery() {
6362
// Take a photo
6463
const capturedPhoto = await Camera.getPhoto({
6564
resultType: CameraResultType.Uri,
6665
source: CameraSource.Camera,
67-
quality: 100
66+
quality: 100,
6867
});
6968
}
7069
}
@@ -83,9 +82,8 @@ import { PhotoService } from '../services/photo.service';
8382
standalone: false,
8483
})
8584
export class Tab2Page {
86-
8785
// update constructor to include photoService
88-
constructor(public photoService: PhotoService) { }
86+
constructor(public photoService: PhotoService) {}
8987

9088
// add addNewToGallery method
9189
addPhotoToGallery() {
@@ -99,9 +97,7 @@ Then, open `tab2.page.html` and call the `addPhotoToGallery()` function when the
9997
```html
10098
<ion-header [translucent]="true">
10199
<ion-toolbar>
102-
<ion-title>
103-
Tab 2
104-
</ion-title>
100+
<ion-title> Tab 2 </ion-title>
105101
</ion-toolbar>
106102
</ion-header>
107103

@@ -118,7 +114,6 @@ Then, open `tab2.page.html` and call the `addPhotoToGallery()` function when the
118114
<ion-icon name="camera"></ion-icon>
119115
</ion-fab-button>
120116
</ion-fab>
121-
122117
</ion-content>
123118
```
124119

@@ -147,7 +142,7 @@ Back at the top of the `PhotoService` class definition, define an array of Photo
147142
export class PhotoService {
148143
public photos: UserPhoto[] = [];
149144

150-
constructor() { }
145+
constructor() {}
151146

152147
// other code
153148
}
@@ -170,6 +165,7 @@ public async addNewToGallery() {
170165
});
171166
}
172167
```
168+
173169
`photo.service.ts` should now look like this:
174170

175171
```tsx
@@ -179,23 +175,23 @@ import { Filesystem, Directory } from '@capacitor/filesystem';
179175
import { Preferences } from '@capacitor/preferences';
180176

181177
@Injectable({
182-
providedIn: 'root'
178+
providedIn: 'root',
183179
})
184180
export class PhotoService {
185181
public photos: UserPhoto[] = [];
186-
constructor() { }
182+
constructor() {}
187183

188184
public async addNewToGallery() {
189185
const capturedPhoto = await Camera.getPhoto({
190186
resultType: CameraResultType.Uri,
191187
source: CameraSource.Camera,
192-
quality: 100
188+
quality: 100,
193189
});
194190

195191
// add new photo to photos array
196192
this.photos.unshift({
197-
filepath: "soon...",
198-
webviewPath: capturedPhoto.webPath!
193+
filepath: 'soon...',
194+
webviewPath: capturedPhoto.webPath!,
199195
});
200196
}
201197
}
@@ -205,14 +201,13 @@ export interface UserPhoto {
205201
webviewPath?: string;
206202
}
207203
```
204+
208205
Next, move over to `tab2.page.html` so we can display the image on the screen. Add a [Grid component](https://ionicframework.com/docs/api/grid) so that each photo will display nicely as photos are added to the gallery, and loop through each photo in the `PhotoServices`'s Photos array, adding an Image component (`<ion-img>`) for each. Point the `src` (source) at the photo’s path:
209206

210207
```html
211208
<ion-header [translucent]="true">
212209
<ion-toolbar>
213-
<ion-title>
214-
Tab 2
215-
</ion-title>
210+
<ion-title> Tab 2 </ion-title>
216211
</ion-toolbar>
217212
</ion-header>
218213

@@ -238,9 +233,9 @@ Next, move over to `tab2.page.html` so we can display the image on the screen. A
238233
<ion-icon name="camera"></ion-icon>
239234
</ion-fab-button>
240235
</ion-fab>
241-
242236
</ion-content>
243237
```
238+
244239
:::note
245240
Learn more about the [ngFor core directive](https://blog.angular-university.io/angular-2-ngfor/).
246241
:::

docs/angular/your-first-app/3-saving-photos.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,17 @@ import { Filesystem, Directory } from '@capacitor/filesystem';
8686
import { Preferences } from '@capacitor/preferences';
8787

8888
@Injectable({
89-
providedIn: 'root'
89+
providedIn: 'root',
9090
})
9191
export class PhotoService {
9292
public photos: UserPhoto[] = [];
93-
constructor() { }
93+
constructor() {}
9494

9595
public async addNewToGallery() {
9696
const capturedPhoto = await Camera.getPhoto({
9797
resultType: CameraResultType.Uri,
9898
source: CameraSource.Camera,
99-
quality: 100
99+
quality: 100,
100100
});
101101

102102
// Save the picture and add it to photo collection
@@ -114,14 +114,14 @@ export class PhotoService {
114114
const savedFile = await Filesystem.writeFile({
115115
path: fileName,
116116
data: base64Data,
117-
directory: Directory.Data
117+
directory: Directory.Data,
118118
});
119119

120120
// Use webPath to display the new image instead of base64 since it's
121121
// already loaded into memory
122122
return {
123123
filepath: fileName,
124-
webviewPath: photo.webPath
124+
webviewPath: photo.webPath,
125125
};
126126
}
127127

@@ -130,25 +130,27 @@ export class PhotoService {
130130
const response = await fetch(photo.webPath!);
131131
const blob = await response.blob();
132132

133-
return await this.convertBlobToBase64(blob) as string;
133+
return (await this.convertBlobToBase64(blob)) as string;
134134
}
135135

136-
private convertBlobToBase64 = (blob: Blob) => new Promise((resolve, reject) => {
137-
const reader = new FileReader();
138-
reader.onerror = reject;
139-
reader.onload = () => {
136+
private convertBlobToBase64 = (blob: Blob) =>
137+
new Promise((resolve, reject) => {
138+
const reader = new FileReader();
139+
reader.onerror = reject;
140+
reader.onload = () => {
140141
resolve(reader.result);
141-
};
142-
reader.readAsDataURL(blob);
143-
});
142+
};
143+
reader.readAsDataURL(blob);
144+
});
144145
}
145146

146147
export interface UserPhoto {
147148
filepath: string;
148149
webviewPath?: string;
149150
}
150151
```
152+
151153
Obtaining the camera photo as base64 format on the web appears to be a bit trickier than on mobile. In reality, we’re just using built-in web APIs: [fetch()](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) as a neat way to read the file into blob format, then FileReader’s [readAsDataURL()](https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL) to convert the photo blob to base64.
152154

153155
There we go! Each time a new photo is taken, it’s now automatically saved to the filesystem. Next up, we'll load and display our saved images
154-
when the user navigates to the Photos tab.
156+
when the user navigates to the Photos tab.

docs/angular/your-first-app/4-loading-photos.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ for (let photo of this.photos) {
7474
photo.webviewPath = `data:image/jpeg;base64,${readFile.data}`;
7575
}
7676
```
77+
7778
After these updates to the `PhotoService` class, your `photos.service.ts` file should look like this:
7879

7980
```tsx
@@ -83,9 +84,8 @@ import { Filesystem, Directory } from '@capacitor/filesystem';
8384
import { Preferences } from '@capacitor/preferences';
8485

8586
@Injectable({
86-
providedIn: 'root'
87+
providedIn: 'root',
8788
})
88-
8989
export class PhotoService {
9090
public photos: UserPhoto[] = [];
9191
private PHOTO_STORAGE = 'photos';
@@ -139,14 +139,14 @@ export class PhotoService {
139139
const savedFile = await Filesystem.writeFile({
140140
path: fileName,
141141
data: base64Data,
142-
directory: Directory.Data
142+
directory: Directory.Data,
143143
});
144144

145145
// Use webPath to display the new image instead of base64 since it's
146146
// already loaded into memory
147147
return {
148148
filepath: fileName,
149-
webviewPath: photo.webPath
149+
webviewPath: photo.webPath,
150150
};
151151
}
152152

@@ -155,17 +155,18 @@ export class PhotoService {
155155
const response = await fetch(photo.webPath!);
156156
const blob = await response.blob();
157157

158-
return await this.convertBlobToBase64(blob) as string;
158+
return (await this.convertBlobToBase64(blob)) as string;
159159
}
160160

161-
private convertBlobToBase64 = (blob: Blob) => new Promise((resolve, reject) => {
162-
const reader = new FileReader();
163-
reader.onerror = reject;
164-
reader.onload = () => {
161+
private convertBlobToBase64 = (blob: Blob) =>
162+
new Promise((resolve, reject) => {
163+
const reader = new FileReader();
164+
reader.onerror = reject;
165+
reader.onload = () => {
165166
resolve(reader.result);
166-
};
167-
reader.readAsDataURL(blob);
168-
});
167+
};
168+
reader.readAsDataURL(blob);
169+
});
169170
}
170171

171172
export interface UserPhoto {
@@ -187,7 +188,6 @@ import { PhotoService } from '../services/photo.service';
187188
standalone: false,
188189
})
189190
export class Tab2Page {
190-
191191
constructor(public photoService: PhotoService) {}
192192

193193
// add call to loadSaved on navigation to Photos tab
@@ -200,6 +200,7 @@ export class Tab2Page {
200200
}
201201
}
202202
```
203+
203204
:::note
204205
If you're seeing broken image links or missing photos after following these steps, you may need to open your browser's
205206
dev tools and clear both [localStorage](https://developer.chrome.com/docs/devtools/storage/localstorage) and [IndexedDB](https://developer.chrome.com/docs/devtools/storage/indexeddb).

docs/angular/your-first-app/5-adding-mobile.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ top of `photo.service.ts`.
7070
```tsx
7171
import { Capacitor } from '@capacitor/core';
7272
```
73+
7374
Then update `savePicture()` to look like the following:
75+
7476
```tsx
7577
// Save picture to file on device
7678
private async savePicture(photo: Photo) {

0 commit comments

Comments
 (0)