如何在角度6中打印数组元素的值作为警报

时间:2018-06-27 12:48:11

标签: angular typescript data-binding

我正在控制台记录来自后端的数组的第0个元素的值。

我的问题是我该如何使用该值并在html页面上打印警告消息。

import { Component, OnInit } from '@angular/core';
import { RegisterService } from '../../services/register.service';
import { Router } from '@angular/router';


@Component({
  selector: 'app-register',
  templateUrl: './register.component.html',
  styleUrls: ['./register.component.css']
})

export class RegisterComponent implements OnInit {
 model: any = {};
 
  tiles = [
    {text: 'Four', cols: 2, rows: 1, color: '#DDBDF1'}
  ];
  constructor(private auth: RegisterService, private router: Router) { }

  ngOnInit() {
  }

  registerUser(event) {
    console.log(event);
    event.preventDefault()
    const errors = []
    const target = event.target
    const username = target.querySelector('#username').value
    const email = target.querySelector('#email').value
    const dob = target.querySelector('#date').value
    const password = target.querySelector('#password').value
    var test;    

      this.auth.registerUser(username,email,dob, password).subscribe(data => {
        console.log(data);
        if(data[0].success) {
            console.log(data[0].success) // this data[0] i need to be on html page
        }else{
        }
      })
  

    console.log(username,email,dob,  password)
   
   
  }

}

我希望将数据[0]打印在html页面上

1 个答案:

答案 0 :(得分:1)

您需要将数据与某个变量绑定,然后就可以像这样访问它了-

this.auth.registerUser(username,email,dob, password).subscribe(data => {
        console.log(data);
        if(data[0].success) {
            this.yourData = data[0].success // this data[0] i need to be on html page
        }else{
        }
      });

并像这样在HTML中绑定表达式-

{{yourData}}