-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular.txt
More file actions
281 lines (139 loc) · 4.54 KB
/
angular.txt
File metadata and controls
281 lines (139 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
angular commands
****************
1)installation guide:
install nodejs:
---------------
https://nodejs.org/en
install angular cli:
--------------------
npm install -g @angular/cli
create project:
--------------
ng new <project_name>
npx @angular/cli@16 new verra-dmrv-client-v16
component creation command:
***************************
ng generate component component-name (ng g c component-name)
componentname standard: componentName (pascalCase)
decorator:
**************
it tells angular about it is a component(identifying a class as a componennt)
@Component({
selector: 'app-admin',
imports: [],
templateUrl: './admin.component.html',
styleUrl: './admin.component.css'
})
selector: it used to find component uniquely
imports:helps what are the nodes needed
templateurl:it tells the template used
styleurl:it tells the styling file
How to import other component in main component?
************************************************
*)include component inside imports .ts file
*)<app-componame></app-componame> include in template file
How to create a single file component?
***************************************
just include app.component.ts
@Component({
selector: 'app-user',
imports: [],
template: `<h1>hello iam user</h1>`,
styles:[`
h1 {
color: red;
font-family: Arial, sans-serif;
}
`]
})
How to import bootstrap in angular?
************************************
visit website: npmjs.org
npm i bootstrap
Data Binding?
**************
*)Types of Data Binding:1)Interpolation,2)Property Binding,3)Event Binding,4)Two-Way Binding
1)interpolation:
*first you should declare variable inside class
export class DataBindingComponent {
name='Alan Sebastian';
age:number=22;
}
*you have to place that inside the tag in template file
<h2>{{name}}</h2>
<p>{{age}}</p>
2)Property Binding:
*first you should declare variable inside class
export class DataBindingComponent {
name='Alan Sebastian';
age:number=22;
}
*)then include in tag
<input type="text" [placeholder]="placeholder" >
3)Event binding:
*)first you stup a event
submit(){
alert("name submited successfully");
}
*)then include in tag
<button (click)="submit()">Submit</button>
4)Two way binding:
*)inorder to apply two way binding first import formmodule
imports: [FormsModule],
*)inside where you used provide [(ngModel)]
<input type="text" [(ngModel)]="name" placeholder="your name">
Structural Directives:
*********************
Structural directives modify the DOM’s structure by adding, removing, or replacing elements. They typically use an asterisk (*) syntax in templates.
ng-if:conditionally render or remove
*)first you need to import CommonModule or ngif
imports: [CommonModule]
ng-for:ng-for is used to iterate through array or arrayObject
<option *ngFor="let color of colorDetails" value="{{color.name}}">{{color.name}}</option>
Attribute Directives:
*********************
*)ng-class:
ngClass is a directive in Angular that is used to dynamically add or remove CSS classes to an HTML element based on certain conditions.
in order to include ngclass first you need to import 'commonmodule' or 'ngclass'
example:
export class NgClassComponent {
displayColor:string="bg-success";
}
<div class="row" [ngClass]="displayColor">
<div class="col-3">
div 2
</div>
</div>
*)ngStyle:ngStyle is a built-in directive that allows you to dynamically set inline CSS styles on an HTML element based on an object or expression.
in order to include ngclass first you need to import 'commonmodule'
example:
export class NgStyleComponent {
divStyle={
backgroundColor: 'red',
color: 'black',
padding: '10px'
};
onchange(color:string){
this.divStyle.backgroundColor=color;
}
<div class="row" [ngStyle]="divStyle">
<div class="col-3">
div 1
</div>
</div>
<div class="row">
<div class="col-3">
<button class="bg-danger" (click)="onchange('red')">show red</button>
<button class="bg-info" (click)="onchange('green')">show green</button>
</div>
</div>
Routing:routing is used to navigate to specific pages
*)setup path inside app.route.ts
export const routes: Routes = [
{ path: 'admin', component:AdminComponent}, // About page
{ path: 'databinding', component: DataBindingComponent}
];
*)how to setup an default route
{ path: '', redirectto:'admin'},
*)Redirecting from one page to another
<button routerLink="/ng-class" class="bg-primary">navigate to ng-class</button>