将嵌套对象添加到Angular2中的常量

时间:2017-03-17 07:20:51

标签: mongodb angular

希望了解为什么这不是按预期工作和可能的解决方法。我运行此函数并尝试将其返回到注册表单上填写的信息,以便稍后附加到我的数据模型。我正在尝试第一个条目也放入默认值,所以他们不会保持为空,因为我使用MongoDB来存储数据。除了"对象"。

之外,数据模型可以看到所有内容

register.component.ts

import { Component, OnInit } from '@angular/core';
import { ValidateService } from '../../services/validate.service';
import { FlashMessagesService } from 'angular2-flash-messages';
import { AuthService } from '../../services/auth.service';
import { Router } from '@angular/router';

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

export class RegisterComponent{
  name: String;
  email: String;
  phonenumber: String;
  username: String;
  password: String;
  objects: Object;
  object1: Object;
  object2: Object;
  foo: String;
  bar: String;

  constructor(private validateService: ValidateService,
    private flashMessage: FlashMessagesService,
    private authService: AuthService,
    private router: Router) { }

onRegisterSubmit(){
  const user = {
    name: this.name,
    email: this.email,
    phonenumber: this.phonenumber,
    username: this.username,
    password: this.password,
    objects: {
      object1: {
        foo: "no foo",
        bar: "no bar"
        },
      object2: {
        foo: "no foo",
        bar: "no bar"
        }
      }
    }

    this.authService.registerUser(user).subscribe(data => {
      if(data.success){
        this.flashMessage.show('You are now registered and can log in', {cssClass: 'alert-success', timeout: 3000});
        this.router.navigate(['/login']);
      } else {
        this.flashMessage.show('Something went wrong', {cssClass: 'alert-danger', timeout: 3000});
        this.router.navigate(['/register']);
      }
    });
  }

register.component.html

<h2 class="page-header">Register</h2>
<form (submit)="onRegisterSubmit()">
  <div class="form-group">
    <label>Name</label>
    <input type="text" [(ngModel)]="name" name="name" class="form-control">
  </div>
  <div class="form-group">
    <label>Username</label>
    <input type="text" [(ngModel)]="username" name="username" class="form-control">
  </div>
  <div class="form-group">
    <label>Email</label>
    <input type="text" [(ngModel)]="email" name="email" class="form-control" >
  </div>
  <div class="form-group">
    <label>Phonenumber</label>
    <input type="text" [(ngModel)]="phonenumber" name="phonenumber" class="form-control" >
  </div>
  <div class="form-group">
    <label>Password</label>
    <input type="password" [(ngModel)]="password" name="password" class="form-control">
  </div>
  <input type="submit" class="btn btn-primary" value="Submit">
</form>

0 个答案:

没有答案