Skip to content

Commit e8acc67

Browse files
add code examples for angular
1 parent 88f45e8 commit e8acc67

File tree

1 file changed

+166
-5
lines changed
  • src/pages/[platform]/build-a-backend/data/set-up-data

1 file changed

+166
-5
lines changed

src/pages/[platform]/build-a-backend/data/set-up-data/index.mdx

Lines changed: 166 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ Future<void> main() async {
360360
</InlineFilter>
361361
## Write data to your backend
362362

363-
<InlineFilter filters={["react", "angular", "javascript", "vue", "nextjs", "react-native"]}>
363+
<InlineFilter filters={["react", "javascript", "nextjs", "react-native", "vue"]}>
364364

365365
Let's first add a button to create a new todo item. To make a "create Todo" API request, generate the data client using `generateClient()` in your frontend code, and then call `.create()` operation for the Todo model. The Data client is a fully typed client that gives you in-IDE code completion. To enable this in-IDE code completion capability, pass in the `Schema` type to the `generateClient` function.
366366

@@ -391,8 +391,44 @@ Run the application in local development mode and check your network tab after c
391391
Try playing around with the code completion of `.update(...)` and `.delete(...)` to get a sense of other mutation operations.
392392

393393
</Callout>
394+
</InlineFilter>
395+
396+
<InlineFilter filters={["angular"]}>
397+
398+
Let's first add a button to create a new todo item. To make a "create Todo" API request, generate the data client using `generateClient()` in your frontend code, and then call `.create()` operation for the Todo model. The Data client is a fully typed client that gives you in-IDE code completion. To enable this in-IDE code completion capability, pass in the `Schema` type to the `generateClient` function.
399+
400+
```tsx title="todo-list.component.ts"
401+
import { Component } from '@angular/core';
402+
import type { Schema } from '../amplify/data/resource';
403+
import { generateClient } from 'aws-amplify/data';
404+
405+
const client = generateClient<Schema>();
406+
407+
@Component({
408+
selector: 'app-todo-list',
409+
template: `
410+
<button (click)="createTodo()">Add new todo</button>
411+
`
412+
})
413+
export class TodoListComponent {
414+
async createTodo() {
415+
await client.models.Todo.create({
416+
content: window.prompt("Todo content?"),
417+
isDone: false
418+
});
419+
}
420+
}
421+
```
422+
423+
Run the application in local development mode and check your network tab after creating a todo. You should see a successful request to a `/graphql` endpoint.
424+
425+
<Callout>
426+
427+
Try playing around with the code completion of `.update(...)` and `.delete(...)` to get a sense of other mutation operations.
394428

429+
</Callout>
395430
</InlineFilter>
431+
396432
<InlineFilter filters={["android"]}>
397433

398434
In your MainActivity, add a button to create a new todo.
@@ -583,9 +619,7 @@ Creating Todo successful.
583619

584620
Next, list all your todos and then refetch the todos after a todo has been added:
585621

586-
<InlineFilter filters={["react", "angular", "javascript", "vue", "nextjs", "react-native"]}>
587-
588-
622+
<InlineFilter filters={["react", "javascript", "vue", "nextjs", "react-native"]}>
589623
```tsx title="src/TodoList.tsx"
590624
import { useState, useEffect } from "react";
591625
import type { Schema } from "../amplify/data/resource";
@@ -628,6 +662,49 @@ export default function TodoList() {
628662
```
629663

630664
</InlineFilter>
665+
666+
<InlineFilter filters={["angular"]}>
667+
```tsx title="todo-list.component.ts"
668+
import { Component, OnInit } from '@angular/core';
669+
import type { Schema } from '../amplify/data/resource';
670+
import { generateClient } from 'aws-amplify/data';
671+
672+
const client = generateClient<Schema>();
673+
674+
@Component({
675+
selector: 'app-todo-list',
676+
template: `
677+
<div>
678+
<button (click)="createTodo()">Add new todo</button>
679+
<ul>
680+
<li *ngFor="let todo of todos">{{ todo.content }}</li>
681+
</ul>
682+
</div>
683+
`
684+
})
685+
export class TodoListComponent implements OnInit {
686+
todos: Schema['Todo']['type'][] = [];
687+
688+
async ngOnInit() {
689+
await this.fetchTodos();
690+
}
691+
692+
async fetchTodos() {
693+
const { data: items } = await client.models.Todo.list();
694+
this.todos = items;
695+
}
696+
697+
async createTodo() {
698+
await client.models.Todo.create({
699+
content: window.prompt('Todo content?'),
700+
isDone: false
701+
});
702+
await this.fetchTodos();
703+
}
704+
}
705+
```
706+
</InlineFilter>
707+
631708
<InlineFilter filters={["android"]}>
632709

633710
Start by creating a new `TodoList` @Composable that fetches the data on the initial display of the TodoList:
@@ -838,7 +915,7 @@ class _MyHomePageState extends State<MyHomePage> {
838915

839916
## Subscribe to real-time updates
840917

841-
<InlineFilter filters={["react", "angular", "javascript", "vue", "nextjs", "react-native"]}>
918+
<InlineFilter filters={["react", "javascript", "vue", "nextjs", "react-native"]}>
842919

843920
You can also use `observeQuery` to subscribe to a live feed of your backend data. Let's refactor the code to use a real-time observeQuery instead.
844921

@@ -893,6 +970,90 @@ You can also use `.onCreate`, `.onUpdate`, or `.onDelete` to subscribe to specif
893970
</Callout>
894971

895972
</InlineFilter>
973+
974+
<InlineFilter filters={["angular"]}>
975+
976+
You can also use `observeQuery` to subscribe to a live feed of your backend data. Let's refactor the code to use a real-time observeQuery instead.
977+
978+
```tsx title="todo-list.component.ts"
979+
import { Component, OnInit } from '@angular/core';
980+
import { CommonModule } from '@angular/common';
981+
import { generateClient } from 'aws-amplify/data';
982+
import type { Schema } from '../../../amplify/data/resource';
983+
import { Subscription } from 'rxjs';
984+
985+
const client = generateClient<Schema>();
986+
987+
@Component({
988+
selector: 'app-todos',
989+
standalone: true,
990+
imports: [CommonModule],
991+
template: `
992+
<main>
993+
<h1>My todos</h1>
994+
<button (click)="createTodo()">+ new</button>
995+
<ul>
996+
<li *ngFor="let todo of todos">
997+
{{ todo.content }}
998+
</li>
999+
</ul>
1000+
<div>
1001+
🥳 App successfully hosted. Try creating a new todo.
1002+
<br />
1003+
<a href="https://docs.amplify.aws/gen2/start/quickstart/">
1004+
Review next steps of this tutorial.
1005+
</a>
1006+
</div>
1007+
</main>
1008+
`,
1009+
})
1010+
export class TodosComponent implements OnInit {
1011+
todos: Schema['Todo']['type'][] = [];
1012+
subscription?: Subscription;
1013+
1014+
ngOnInit(): void {
1015+
this.listTodos();
1016+
}
1017+
1018+
ngOnDestroy(): void {
1019+
this.subscription?.unsubscribe();
1020+
}
1021+
1022+
listTodos() {
1023+
try {
1024+
this.subscription = client.models.Todo.observeQuery().subscribe({
1025+
next: ({ items, isSynced }) => {
1026+
this.todos = items;
1027+
},
1028+
});
1029+
} catch (error) {
1030+
console.error('error fetching todos', error);
1031+
}
1032+
}
1033+
1034+
createTodo() {
1035+
try {
1036+
client.models.Todo.create({
1037+
content: window.prompt('Todo content'),
1038+
});
1039+
this.listTodos();
1040+
} catch (error) {
1041+
console.error('error creating todos', error);
1042+
}
1043+
}
1044+
}
1045+
```
1046+
1047+
Now try to open your app in two browser windows and see how creating a todo in one window automatically adds the todo in the second window as well.
1048+
1049+
<Callout>
1050+
1051+
You can also use `.onCreate`, `.onUpdate`, or `.onDelete` to subscribe to specific events. Review [Subscribe to real-time events](/[platform]/build-a-backend/data/subscribe-data) to learn more about subscribing to specific mutation events.
1052+
1053+
</Callout>
1054+
1055+
</InlineFilter>
1056+
8961057
<InlineFilter filters={["android"]}>
8971058

8981059
To add real-time updates, you can use the subscription feature of Amplify Data. It allows to subscribe to `onCreate`, `onUpdate`, and `onDelete` events of the application. In our example, let's append the list every time a new todo is added.

0 commit comments

Comments
 (0)