0

i wanted to fetch data from the API and print the same data on the HTML page.

I have the code as below. I am able to fetch data from the API and console log the API data. But I am unable to print the same on HTML page.

there is no errors that are shown while compiling or while executing the code, i think the code is missing some thing . i couldn't see any mistake as of now, please help me out in solving this the component.ts, HTML, Service.ts are given below

component.ts

export class LoginComponent implements OnInit {

    username = '';
    password = '';
    a = '';
    data1 = [];

    constructor(private abc: LoginService, private router: Router) {}

    ngOnInit() {}

    login() {
        console.log(JSON.stringify({
            username: this.username,
            password: this.password
        }));
        this.a = JSON.stringify({
            username: this.username,
            password: this.password
        });
        this.abc.getValues(this.a).subscribe((data: any) => {
            this.data1 = data;
            console.log(data);
            console.log(this.data1);
        });
    }

}

component.html

<div class="wrapper fadeInDown">
    <div id="formContent">
        <!-- Tabs Titles -->
        <!-- Icon -->
        <div class="fadeIn first">
            <h1>Touchworld Technologies</h1>
        </div>
        <!-- Login Form -->
        <form #formData="ngForm">
            <input type="text" id="login" class="fadeIn second" placeholder="username" name="username"
                [(ngModel)]="username">
            <input type="text" id="password" class="fadeIn third" name="password" placeholder="password"
                [(ngModel)]="password">
            <input type="submit" class="fadeIn fourth" value="Log In" (click)="login()">
            <!-- <button class="fadeIn fourth" [routerLink]="['../secondpage']">Home</button> -->
        </form>
        {{data1}}
        <!-- Remind Passowrd -->
        <div id="formFooter">
            <a class="underlineHover" href="#">Go to the Site</a>
        </div>
    </div>
</div>

<ul>
    <li *ngFor="let datas of data1">
        <span>{{datas.id}}</span>
        <span>{{datas.title}}</span>
        <span>Test</span>
    </li>
</ul>

service.ts

export class LoginService {

    constructor(private http: HttpClient) {}

    getValues(a: any) {
        console.log("you have entered");
        console.log(a);
        return this.http.get('https://jsonplaceholder.typicode.com/posts');
    }

}
2
  • You can print JSON with JsonPipe. That being said, as the answers describe, you are trying to render a variable that may have not been declared or defined. Commented Jan 30, 2020 at 5:57
  • i have tried JsonPipe also, still not working Commented Jan 30, 2020 at 6:03

1 Answer 1

1

You can print data1 on HTML side as below in component.html:

{{data1 | json}}

You can check ref here

Sign up to request clarification or add additional context in comments.

2 Comments

Please make sure you import CommonModule in app.module.ts
imported CommonModule and i am able to console log the api data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.