选择ngmodel不绑定变量

时间:2018-02-04 22:06:17

标签: angular angular2-ngmodel

我有一个非常简单的页面,其中两个选项填入相同的数据(银行帐户)。一个旨在成为源和另一个目标,以创建一个交易。在这种简单的场景中,交易只是从一个账户转移到另一个账户的金额。

我在将交易发送到我的休息服务时遇到问题。从图像中可以很容易地看出,交易变量没有得到适当的填充,但我不知道出了什么问题。

我看到事务部分填充的第一个帐户(来源)和第二个(目标)的[对象]。为什么没有填充相同,因为它们是完全相同类型的对象,为什么在第一个名称中没有填充?

新transaction.html:

<div>
  <div>
  <label>Source Account</label>
  <select [(ngModel)]="transaction.sourceAccount"> 

      <option *ngFor="let a of accounts" [value]="a" >{{a.name}}</option>
  </select>
</div>
<div>
  <label>Target Account</label>
  <select  [(ngModel)]="transaction.targetAccount" > 
      <option *ngFor="let a of accounts" [value]="a">{{a.name}}</option>
  </select>
</div>
<div>
  <input type="text" [(ngModel)]="transaction.amount">
</div>
<div>
    <button (click)="addTransaction()">Add</button>
 </div>
</div>

新transaction.component.ts

...
export class NewTransactionComponent implements OnInit {

  accounts: Account[];
  transaction: Transaction = new Transaction();

  constructor(private accountService: AccountService, private transactionService: TransactionService) { }

  ngOnInit(): void {
    this.transaction.targetAccount = new Account();
    this.transaction.sourceAccount = new Account();

    this.accountService
    .getAllAccounts()
    .subscribe(
      (accounts) => {
        this.accounts = accounts;
      }
    );
  }

  addTransaction(): void {
    this.transactionService.addTransaction(this.transaction)    
    .subscribe(
      (transaction) => {
        this.transaction = transaction;
      }
    );
    //this.router.navigate(['/home']);
  } 
}

transaction.service.ts

...

  public addTransaction(transaction: Transaction): Observable<Transaction> {
    return this.http
      .post(API_URL + '/transaction', transaction)
      .map(response => {
        return new Transaction(response.json());
      })
      .catch(this.handleError);
  }

transaction.ts

import { Account } from "./account";

export class Transaction {
    idtransaction: number;
    amount: number;
    sourceAccount: Account;
    targetAccount: Account;

    constructor(values: Object = {}) {
      Object.assign(this, values);
    }
}

account.ts

import { User } from "./user";

export class Account {
    id: number;
    name: string = '';
    user: User[] = [];

    constructor(values: Object = {}) {
      Object.assign(this, values);
    }
}

enter image description here

***编辑

enter image description here

2 个答案:

答案 0 :(得分:3)

我认为你错过了使用ngValue指令来获取对象 反映在ngModel的绑定值中。

<label>Target Account</label>
<select  [(ngModel)]="transaction.targetAccount" > 
    <option *ngFor="let a of accounts" [ngValue]="a">{{a.name}}</option>
</select>

从文档.. 如果您的选项值恰好是对象(并且您希望将表单中的选择保存为对象),请使用ngValue

请点击此处查看select element.

的完整文档

注意我不确定为什么它似乎在处理sourceAccount属性。

答案 1 :(得分:1)

我使用以下代码进行了此操作。我喜欢Garth发布的内容,而且代码也会少一些。我看到他的答案后几分钟就准备好了,所以我要发布它,因为我花了一些时间。

新transaction.component.html

<div>
  <div>
  <label>Source Account</label>
  <select [(ngModel)]="sourceBankAccount" name="sourceBankAccount" (change)="sourceAccountChanged($event.target.value)" >

      <option *ngFor="let a of accounts" [value]="a.id" >{{a.name}}</option>
  </select>
</div>
<div>
  <label>Target Account</label>
  <select  [(ngModel)]="targetBankAccount"  name="targetBankAccount" (change)="targetAccountChanged($event.target.value)" >
      <option *ngFor="let a of accounts" [value]="a.id">{{a.name}}</option>
  </select>
</div>
<div>
  <input type="text" [(ngModel)]="transaction.amount">
</div>
<div>
    <button (click)="addTransaction()">Add</button>
 </div>
</div>

请注意,这会使用下拉列表中的值的帐户ID。此处也有change个事件调用。

新transaction.component.ts

export class NewTransactionComponent implements OnInit {

  accounts: BankAccount[];
  transaction: Transaction = new Transaction();
  sourceBankAccount: number;
  targetBankAccount: number;

  constructor(private accountService: BankAccountService, private transactionService: TransactionService) { }

  ngOnInit(): void {
    this.accountService
    .getAllAccounts()
    .subscribe(
      (accounts) => {
        this.accounts = accounts;
      }
    );
  }

  sourceAccountChanged(account: number) {
    const newAccount: BankAccount = this.accounts.find(acct => acct.id === +account);
    if (newAccount) {
      this.transaction.sourceAccount = new BankAccount(newAccount);
    }
  }

  targetAccountChanged(account: number) {
    const newAccount: BankAccount = this.accounts.find(acct => acct.id === +account);
    if (newAccount) {
      this.transaction.targetAccount = new BankAccount(newAccount);
    }
  }

  addTransaction(): void {
    console.log(this.transaction);
    this.transactionService.addTransaction(this.transaction)
    .subscribe(
      (transaction) => {
        this.transaction = transaction;
      }
    );
  }
}