The form fails to appear if there are no values entered

Having trouble with my template not displaying when the details are empty, but it shows up fine when there are values present. I'm stuck and unable to find a solution on my own. Can someone please offer some assistance?

<div  class = "nopadding col-sm-12">
<form  class="nobottommargin"   [formGroup]="form"  (ngSubmit)="onSubmit(form.value)" name="template-contactform">
<div class="col-sm-12 nopadding socialaddress">
    <div class="col-sm-12 formpaddingcss">
       <h3 class = "headingfontcss">SOCIAL ADDRESS</h3>
     </div>  
        <div class="col-sm-12 formpaddingcss cardbackgroundcolor">
            <div class="col-sm-7 cardtopbottompadding noleftpadding">
                <div class="col-sm-12 nopadding">
                   <label for="template-contactform-name" class="col-sm-5 nopadding">FACEBOOK <small>*</small></label>
                   <div class="col-sm-7 nopadding text-right">
                       <span  class="cardtextcolor" tooltip="Log in to your facebook account and click on your profile, copy and paste url from there" [tooltipDisabled]="false" [tooltipAnimation]="true"
                    tooltipPlacement="top"><i class="icon-question-sign"></i> What is my address?</span>
                   </div>   
                </div>   
                <div class="input-group divcenter">
                    <span class="input-group-addon noradius inputgroupaddon"><i class="icon-facebook"></i></span>
                    <input type="email" tooltip="Enter Facebook url" [tooltipDisabled]="false" [(ngModel)]="details.facebook"  class="form-control required email formcontrolheight" placeholder="Facebook" aria-required="true">
                </div>
            </div>
      </form>

My ts,

export class Social {
      message: any;
      http: Http;
      details: IDetails[];
      form: FormGroup;

     constructor(fbld: FormBuilder, http: Http, private _service: GetAllList,public toastr: ToastsManager) {
    this.http = http;
      this._service.getList()
        .subscribe(details => this.details = details);
      }

Http call ts,

getList(): Observable<IDetails[]> {
  //  console.log(this.id);
    return this._http.get(this._productUrl)
        .map((response: Response) => {
            if(response.json().error_code ==0){
            return <IDetails[]>response.json().data[0];}

        });

}

}

Answer №1

It seems like Angular2 is throwing an exception because of

[(ngModel)]="details.facebook"

The issue arises from trying to access .facebook when details is null

To resolve this, you can modify the code to:

[ngModel]="details?.facebook" (ngModelChange)="details.facebook = $event"

Alternatively, you can use the following approach:

<input type="email" *ngIf="details"

By making these adjustments, the code should function as intended.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Identifying @Prop() modifications using stencils

Is there a way to detect property changes in Stencil? In Angular, it's done like this: However, I'm unsure of how to achieve the same in Stencil. import { Component, h } from '@stencil/core'; @Component({ tag: 'my-name', ...

Dynamic side menu change in Ionic 3 allows for flexibility and customization in the app's

Is there a way to automatically change the direction of the side menu when switching between languages (rtl and ltr)? I attempted to implement this functionality in the app.html page using the following code: <ion-menu [side]="isRtl?'right':& ...

NGRX - Actions should always define a specific "type" property and not leave it undefined

I am attempting to make this ngrx @Effect work with an http request. @Effect() loadPosts$ = this.actions$ .ofType(post.ActionTypes.LOAD) .map(() => { return this.postService.get() .map(posts => { return new post.L ...

Components loading in Angular result in lat long being undefined in google map integration

I am currently utilizing Ionic-angular AGM. I am facing an issue where I am unable to retrieve my current location when the component loads, as it seems like the component is being fired before the latitude and longitude are ready. How can I make this proc ...

Retrieving information from a child modal window in an Angular parent component

In the scenario where there is data in two tables on the left and right sides, a modal popup window will open when a user clicks a link from the parent component. Upon selecting a radio button from the window, it should correspond to the selected link in t ...

Guide to obtaining ngPrime autocomplete text when the button is clicked within Angular 6

I am currently utilizing the ngPrime autocomplete feature to retrieve data from a service. My goal is to capture the text entered in the autocomplete field whenever a button is clicked. I attempted to access the value using getElementById.value within th ...

Issue arises while extracting a conditional type from a generic function

I've come across a TypeScript error while trying to implement a conditional type in a MessagesStore object. Here is the code snippet: type Message = { id: number; text: string }; type MessagesStore = { queue: Array<Message>; read: <T ...

Update the language setting on-the-fly in Angular 7 without the need to refresh the application

Is there a way to change the Locale_Id in my angular app during runtime without using window.location.reload() to update the locale? I need to implement a solution where I can switch locales dynamically without having to reload the entire application. Be ...

Encountering a TS(2322) Error while Implementing Observables in Angular 12

Exploring the intricacies of Angular 12 framework, I find myself encountering a hurdle in my learning journey. The tutorial I am following utilizes the Observable class to query fixed data asynchronously. However, an unexpected ts(2322) error has surfaced ...

The installed NPM package does not contain the necessary TypeScript compiled JS files and declaration files

I have recently released a TypeScript library on NPM. The GitHub repository's dist (Link to Repository Folder) directory includes all compiled JavaScript and d.ts files. However, after running npm i <my_package>, the resulting module contains on ...

Exploring Angular 10: Managing Two Promises in ngOnInit

I am currently working on integrating the Strava API into my Angular app. To summarize briefly: When a user clicks on a button to connect to Strava They are redirected to Strava for authentication (using PKCE) Strava then redirects back to my app with a ...

The compatibility between TypeScript and the Node.js crypto module is currently not fully optimized

Incorporating encryption into my project using vuejs and typescript has been a challenge. I managed to implement it in the .vue file successfully, but encountered an issue when trying to write the encryption into a typescript class. The mocha test runs fin ...

Utilizing the Pub/Sub architecture to integrate the kafka-node library within Node Js

Utilizing the kafka-node module in my NodeJs Microservise project, I am aiming to implement a Pub/Sub (publisher and subscriber) design pattern within the Functional programming paradigm. producer.js const client = new kafka.KafkaClient({ kafkaHost: ...

How to dynamically adjust column width based on maximum content length across multiple rows in CSS and Angular

I am attempting to ensure that the width of the label column in a horizontal form is consistent across all rows of the form based on the size of the largest label. Please refer to the following image for clarification: Screenshot Example All label column ...

Setting a maximum value for a text box input in Angular for enhanced bot functionality

Currently, I am working with Angular 7 and I find myself trying to incorporate the min and max properties into a textbox. However, I seem to be encountering some difficulties in achieving this. <div> <input type='text' [(ngModel)]= ...

"Type 'Unknown' cannot be assigned to the specified type: Typescript Error encountered while using

I encountered an error message in my Redux Observable setup. Any advice on how to resolve this issue? The error states: 'Argument of type 'OperatorFunction<ITodo[], Action<{ params: { url: string; }; } & { result: { todos: ITodo[]; }; ...

Encountering a roadblock when trying to incorporate jQuery UI into TypeScript

I've been exploring Typescript to improve my coding skills. Currently, I have a UI that's functional in jQuery. My goal is to transition this UI to Typescript. However, I'm facing some challenges as I try to encapsulate everything within the ...

Having trouble accessing a class variable in Angular 8 that is defined inside the FileReader's onloadend method?

Having trouble accessing a class variable that is set inside the FileReader onloadend method. Below is the code snippet: analyzeData(){ let file = this.fileRestful[0]; let fileReader: FileReader = new FileReader(); fileReader.onloadend = () => { thi ...

The error message in React JS Typescript states: "Cannot assign a string to a parameter that requires a SetStateAction<number>"

Hey there! I encountered this error: Argument of type 'string' is not assignable to parameter of type 'SetStateAction<number>'. Here is a snippet of my code where the error occurred: . . . const[ idPadre, setIdPadre ] = useState& ...

What is the best way to update data in real-time following a POST request?

I have a table displaying data. While the POST and GET requests are functioning properly, I am only able to see the values after insertion upon refreshing the page. xyz.service.ts createCategory(options) { return this.http.post<{ message: string } ...