首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将我的数据库中的数据放入angular中的select

如何将我的数据库中的数据放入angular中的select
EN

Stack Overflow用户
提问于 2017-12-10 01:27:42
回答 3查看 44关注 0票数 0

我想让用户能够选择他们预先输入的电子邮件,但是当我使用ng-options并且我尝试了ngFor,但它似乎仍然不起作用,我做错了吗?

transfer.component.html

代码语言:javascript
复制
<form (ngSubmit)="transFunds()" >
  <div class="container">
    <div class="row">
        <div class="col-sm-6 col-sm-offset-3">
          <select [ngModel]="selectedName" (ngModelChange)="updateSelectedValue($event)">
              <option *ngFor ="let recipient of recipients" value="{{recipient.emailTo}}">{{recipient.emailTo}}</option>
        </select>
        </div>  
    </div>
    <div class="row">
        <div class="row">
          <p><input type="number"  placeholder="Amount Transfering" [(ngModel)]="amount"></p>
        </div>  
      </div>  
     <div class="row">
      <div class="col-sm-6 col-sm-offset-3">
        <p><button class="btn btn-primary" type="submit">Send</button> </p>    
      </div>
    </div>
   </div>
  </form>

transfer.component.ts

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
import { AuthService } from '../auth.service';
import { FormBuilder, FormGroup } from '@angular/forms';
import { Router } from '@angular/router';
import { Http } from '@angular/http';
import { PostsService } from '../posts.service';

@Component({
  selector: 'app-transfer',
  templateUrl: './transfer.component.html',
  styleUrls: ['./transfer.component.css']
})
export class TransferComponent implements OnInit {

  constructor(private fb: FormBuilder, private http: Http, private router: Router, private auth: AuthService,private postsService: PostsService) { }
  transfer = {};
  recipients: any;
  transferForm: FormGroup;
  public userEmail: string;

  ngOnInit() {
    this.transferForm = this.fb.group({
      emailTo: '',
      amount: ''
    });
    this.auth.currentEmail.subscribe(email => this.userEmail = email);

    this.postsService.getAllRecipient().subscribe(recipient => {
      this.recipients = recipient;
    });
  }      

}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-12-10 01:33:34

我喜欢在我的代码中使用angular js2中提供的*ngfor

代码语言:javascript
复制
<select [(ngModel)]="selectedName"="selectedName"   
    (ngModelChange)="updateSelectedValue($event)">
        <option *ngFor ="let recipient of recipients" 
           value="{{recipient.email}}">{{recipient.email}}</option>
</select>

我认为需要将value="{{recipient.email}}"替换为接收方Id,因此它将类似于value="{{recipient.Id}}"

票数 0
EN

Stack Overflow用户

发布于 2017-12-10 01:30:47

你把angularjs和angular语法搞混了,应该是,

代码语言:javascript
复制
 <select class="form-control" name="someDrpDown" [(ngModel)]="selectedName">
        <option *ngFor="let recpient of recipients" [ngValue]="recipient">
          {{recpient.email}}
        </option> 
  </select>
票数 1
EN

Stack Overflow用户

发布于 2017-12-10 04:09:35

或者,您可以创建一个自定义指令,如*ngFor。

也就是说,实现我自己的能够遍历ES6可迭代对象的ngFor指令。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47731195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档