简体   繁体   English

Angular 2.0形成回调和逻辑问题

[英]Angular 2.0 forms callback and logic issue

I am very new to Angular, i am truing to create a simple script to update the price when the value of a form input gets changed. 我对Angular非常陌生,我正努力创建一个简单的脚本来在表单输入的值更改时更新价格。 There seems to be several different methods for using Angular with class driven and directive driven models. 在类驱动和指令驱动的模型中使用Angular似乎有几种不同的方法。 Read the documentation from the main angular website, but no luck. 请从主要的角度网站阅读文档,但是没有运气。

import { Component } from '@angular/core';
export class Ticket {
  ticketNumber: number;
  name: string;
  price: number;
}
@Component({
  selector: 'my-app',
  template: `
    <form name="myForm">
    <div class="ticket box_shadows">
       <div class="ticket_title">{{ticket.name}}</div>
        <input type="number" name="myInput" class="ticket_ammount" min="1" max="10" value="{{ticket.ticketNumber}}"required ng-class="updatePrice(price,ticketNumber)">
        <div class="ticket_details"><input class="ticket_details_input" type="text"  placeholder="Name:" required></div>
        <div class="ticket_details"><input class="ticket_details_input" type="email" placeholder="your@email.com" required></div>
        <div class="ticket_details"><input class="ticket_details_input" type="text"  placeholder="+852" required></div>
        <div class="ticket_price">$ <input type="number" name="price" value="{{ticket.price}}|>
        <button type="button" class="ticket_button">Book</button>
        </div>
    </div>
    </form>
    `
})
//creates the class
export class AppComponent {
  ticket: Ticket = {
    ticketNumber: 1,
    name: 'Ticket',
    price: 300 
  };
  //updates the price on a new click
  updatePrice(value, ticketN){
      value = value * ticketN;
      return value; 
      // this then needs to be passed back to the price input field. 
  }
}

----- UPDATE ------- so i was having issues with calling two class's, and trying to make them interact. -----更新-------所以我在调用两个类并尝试使它们交互时遇到问题。 I striped out the code and started again this is what i put in to make it work. 我剥离了代码,然后再次开始,这就是我投入的工作。

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `
    <form>
    <div class="ticket box_shadows">
        <div class="ticket_top">
            <div class="ticket_title">{{title}}</div>
          <input type="number" name="myInput" class="ticket_ammount" min="1" max="10" [(ngModel)]="ticket" (click)="updatePrice()" required>
        </div>
        <div class="ticket_btm">
            <div class="ticket_price"><span>$</span><input type="number" [(ngModel)]="totalPrice" class="ticket_price_number" disabled></div>
            <div class="ticket_btm_spacer"></div>
            <button type="button" class="ticket_button">Book</button>
        </div>
    </div>
    </form>
    `
})
export class AppComponent {
  ticket: number = 1;
  title: string = "Ticket";
  price: number = 300;
  totalPrice: number = 300; 
  updatePrice(){
    this.totalPrice = this.price * this.ticket;
    if(this.ticket >= 2){
      this.title = "Tickets";
    }
    else{ this.title = "Ticket";}
  } 
}

要以两种方式将值绑定到输入,您需要使用ngModel

<input [(ngModel)]="ticket.price">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM