Angular @ViewChild() error: Expected 2 arguments, but got 1
In Angular 8 , ViewChild takes 2 parameters @ViewChild(ChildDirective, {static: false}) Component
In Angular 8 , ViewChild takes 2 parameters @ViewChild(ChildDirective, {static: false}) Component
Update We can just create directive like *ngIf and call it *ngVar ng-var.directive.ts @Directive({ selector: ‘[ngVar]’, }) export class VarDirective { @Input() set ngVar(context: unknown) { this.context.$implicit = this.context.ngVar = context; if (!this.hasView) { this.vcRef.createEmbeddedView(this.templateRef, this.context); this.hasView = true; } } private context: { $implicit: unknown; ngVar: unknown; } = { $implicit: null, ngVar: null, … Read more
So the answer is no, you don’t. Ng2 will clean it up itself. The Http service source, from Angular’s Http XHR backend source: Notice how it runs the complete() after getting the result. This means it actually unsubscribes on completion. So you don’t need to do it yourself. Here is a test to validate: fetchFilms() … Read more
Here is a working example for file upload to api: Step 1: HTML Template (file-upload.component.html) Define simple input tag of type file. Add a function to (change)-event for handling choosing files. <div class=”form-group”> <label for=”file”>Choose File</label> <input type=”file” id=”file” (change)=”handleFileInput($event.target.files)”> </div> Step 2: Upload Handling in TypeScript (file-upload.component.ts) Define a default variable for selected file. … Read more
You missed the * in front of NgIf (like we all have, dozens of times): <div *ngIf=”answer.accepted”>✔</div> Without the *, Angular sees that the ngIf directive is being applied to the div element, but since there is no * or <template> tag, it is unable to locate a template, hence the error. If you get … Read more
Using ng serve –host 0.0.0.0 will allow you to connect to the ng serve using your ip instead of localhost. EDIT In newer versions of the cli, you have to provide your local ip address instead EDIT 2 In newer versions of the cli (I think v5 and up) you can use 0.0.0.0 as the … Read more
The HttpClient methods allow you to set the params in it’s options. You can configure it by importing the HttpClientModule from the @angular/common/http package. import {HttpClientModule} from ‘@angular/common/http’; @NgModule({ imports: [ BrowserModule, HttpClientModule ], declarations: [ App ], bootstrap: [ App ] }) export class AppModule {} After that you can inject the HttpClient and … Read more
You need to explicitly tell TypeScript the type of the HTMLElement which is your target. The way to do it is using a generic type to cast it to a proper type: this.countUpdate.emit((<HTMLTextAreaElement>e.target).value./*…*/) or (as you like) this.countUpdate.emit((e.target as HTMLTextAreaElement).value./*…*/) or (again, matter of preference) const target = e.target as HTMLTextAreaElement; this.countUpdate.emit(target.value./*…*/) This will let … Read more
There is a lot of confusion on this topic because there are so many different ways to do it. Here are the appropriate types used in the following screen shots: private route: ActivatedRoute private router: Router 1) Required Routing Parameters: 2) Route Optional Parameters: 3) Route Query Parameters: 4) You can use a service to … Read more
<div (window:resize)=”onResize($event)” onResize(event) { event.target.innerWidth; } or using the HostListener decorator: @HostListener(‘window:resize’, [‘$event’]) onResize(event) { event.target.innerWidth; } Supported global targets are window, document, and body. Until https://github.com/angular/angular/issues/13248 is implemented in Angular it is better for performance to subscribe to DOM events imperatively and use RXJS to reduce the amount of events as shown in some … Read more