Categories
Angular Answers

How to write data attributes using Angular?

Sometimes, we want to write data attributes using Angular.

In this article, we’ll look at how to write data attributes using Angular.

How to write data attributes using Angular?

To write data attributes using Angular, we can use the att attribute in our element.

For instance, we write

<ol class="viewer-nav">
  <li *ngFor="let section of sections" attr.data-value="{{ section.value }}">
    {{ section.text }}
  </li>
</ol>

to add the data-value attribute into our li elements with

attr.data-value="{{ section.value }}"

Conclusion

To write data attributes using Angular, we can use the att attribute in our element.

Categories
Angular Answers

How to fix Angular error “Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input'”?

Sometimes, we want to fix Angular error "Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’".

In this article, we’ll look at how to fix Angular error "Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’".

How to fix Angular error "Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’"?

To fix Angular error "Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’", we need to add the FormsModule and ReactiveFormsModule into our NgModule.

For instance, we write

import { BrowserModule } from "@angular/platform-browser";
import { FormsModule, ReactiveFormsModule } from "@angular/forms";
import { NgModule } from "@angular/core";
import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";

@NgModule({
  declarations: [AppComponent],
  imports: [FormsModule, ReactiveFormsModule, BrowserModule, AppRoutingModule],
  providers: [],
  bootstrap: [AppComponent],
})
export class AppModule {}

in app.module.ts to add the FormsModule and ReactiveFormsModule into our imports array in the AppModule.

Then we should be able to use ngModel in our component templates.

Conclusion

To fix Angular error "Can’t bind to ‘ngModel’ since it isn’t a known property of ‘input’", we need to add the FormsModule and ReactiveFormsModule into our NgModule.

Categories
Angular Answers

How to return data from subscribe with Angular?

Sometimes, we want to return data from subscribe with Angular.

In this article, we’ll look at how to return data from subscribe with Angular.

How to return data from subscribe with Angular?

To return data from subscribe with Angular, we can return the map observable.

For instance, we write

export class Component {
  someMethod() {
    return this.http.get(path).map((res) => {
      return res.json();
    });
  }
}

to call http.get to make a get request to the path.

Then we call map with a callback that returns the response JSON with res.json.

We return the map observable so we can call someMethod and chain that with subscribe to listen for emitted values from map to get the response.

Conclusion

To return data from subscribe with Angular, we can return the map observable.

Categories
Angular Answers

How to read response headers from API response with Angular and TypeScript?

Sometimes, we want to read response headers from API response with Angular and TypeScript.

In this article, we’ll look at how to read response headers from API response with Angular and TypeScript.

How to read response headers from API response with Angular and TypeScript?

To read response headers from API response with Angular and TypeScript, we can set the observer option to 'response'.

For instance, we write

http.get<any>(url, { observe: "response" }).subscribe((resp) => {
  console.log(resp.headers.get("X-Token"));
});

in a method to make a GET request with http.get.

We call it with the url to make the request to and an object with the observer property set to 'response' to get the response.

In the subscribe callback, we get the response header by the key with resp.headers.get.

Conclusion

To read response headers from API response with Angular and TypeScript, we can set the observer option to 'response'.

Categories
Angular Answers

How to create a custom form input with Angular?

Sometimes, we want to create a custom form input with Angular.

In this article, we’ll look at how to create a custom form input with Angular.

How to create a custom form input with Angular?

To create a custom form input with Angular, we can create a component that implements the ControlValueAccessor class.

For instance, we write

import { Component, Provider, forwardRef, Input } from "@angular/core";
import {
  ControlValueAccessor,
  NG_VALUE_ACCESSOR,
  CORE_DIRECTIVES,
} from "@angular/common";

const CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR = new Provider(NG_VALUE_ACCESSOR, {
  useExisting: forwardRef(() => InputField),
  multi: true,
});

@Component({
  selector: "inputfield",
  template: `<input [(ngModel)]="value" />`,
  directives: [CORE_DIRECTIVES],
  providers: [CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR],
})
export class InputField implements ControlValueAccessor {
  private _value: any = "";
  get value(): any {
    return this._value;
  }

  set value(v: any) {
    if (v !== this._value) {
      this._value = v;
      this.onChange(v);
    }
  }

  writeValue(value: any) {
    this._value = value;
    this.onChange(value);
  }

  onChange = (_) => {};
  onTouched = () => {};
  registerOnChange(fn: (_: any) => void): void {
    this.onChange = fn;
  }
  registerOnTouched(fn: () => void): void {
    this.onTouched = fn;
  }
}

to create the InputField class.

In it, we have a getter and setter to get the set the input value respectively.

In the template, we have an input that binds the input value to the value variable.

Also, we add the registerOnChange and registerOnTouched methods, to add the change and touch event functions.

We also set providers to [CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR] so that we can use the component as a custom input that can bind to input values in template and reactive forms.

Conclusion

To create a custom form input with Angular, we can create a component that implements the ControlValueAccessor class.