简体   繁体   English

角度视图模型及其在组件中的用法

[英]Angular Viewmodels and usage in components

I'm trying to bind viewmodels to a view through component. 我正在尝试通过组件将视图模型绑定到视图。 Initially I did this in the component and it was almost working: 最初,我是在组件中执行此操作的,并且几乎可以正常工作:

model: any = {
        TenantId: '',
        CustomFieldName: '',
        quantities: []
    };

quantity: any = {
        price: '',
        name: ''
    }

Then on button click for adding new object of 然后在按钮上单击添加新对象

model 模型

I was doing this: 我正在这样做:

addNewQuantity() {
        if (this.newQuantity) {
            this.quantity.name = this.newQuantity;
            this.model.quantity.push(this.quantity);
            this.newQuantity = '';
        }
    }

The issue with above event was that the same quantity object was added for every new click. 上述事件的问题在于,每次新点击都添加了相同数量的对象。

What I did next is that I created two models: 接下来,我创建了两个模型:

Pricegridquantity viewmodel: 

export class PriceGridQuantity {
    price: string;
    name: string; 
}

Pricegrid viewmodel Pricegrid视图模型

 import { PriceGridQuantity } from './pricegridquantity';    
 export class PriceGridModel {  
    TenantId: number;
    CustomFieldName:string;  
    PriceList: Array <PriceGridQuantity> ; }

Now I'm trying to link everything in the component, I have this: 现在,我尝试链接组件中的所有内容,我有以下内容:

model: PriceGridModel[] = []; 模型:PriceGridModel [] = [];

Is the statement above correct to initialize a new VM of type PriceGridModel? 上面的语句对于初始化PriceGridModel类型的新VM是否正确?

If no, please let me know what is better. 如果没有,请告诉我哪个更好。

And 2nd, on the button click to add new object of type PriceGridQuantity to PriceGridModel, what is the correct way of initializing and then adding object of type PriceGridQuantity to the PricegridModel? 然后,第二次单击按钮,将类型为PriceGridQuantity的新对象添加到PriceGridModel中,什么是初始化然后将类型为PriceGridQuantity的对象添加到PricegridModel的正确方法?

The reason for the list of quantities being the same is because you have an object called quantity accessible to the entire view via this declaration: 数量列表相同的原因是因为您有一个称为数量的对象,可通过此声明访问整个视图:

quantity: any = {
    price: '',
    name: ''
}

So in a view if you have the ([ngModel])=quantity.name or ([ngModel])=quantity.price it will be bound to the values in quantity. 因此,在视图中,如果具有([ngModel])=quantity.name([ngModel])=quantity.price ,它将绑定到数量上的值。 This is problematic because you are setting the value quantity.name in the addNewQuantity function. 因为你设置的值这是有问题quantity.name在addNewQuantity功能。

addNewQuantity() {
    if (this.newQuantity) {
        this.quantity.name = this.newQuantity;
        this.model.quantity.push(this.quantity);
        this.newQuantity = '';
    }
}

So any template that is set with ([ngModel])=quantity.name will now have the value set in the previous function. 因此,使用([ngModel])=quantity.name设置的任何模板现在都将具有在先前函数中设置的值。

A better implementation is to have a newQuantityObject and an object that has an array property that stores each newly added quantity. 更好的实现方法是有一个newQuantityObject和一个具有数组属性的对象,该属性存储每个新添加的数量。 Then you don't need any new classes to implement. 然后,您不需要任何新的类即可实现。 For example: 例如:

newQuantity = {
    price: '',
    name: ''
};

model: any = {
    TenantId: '',
    CustomFieldName: '',
    quantities: []
};


addNewQuantity() {
    if (this.newQuantity.price !== '' && this.newQuantity.name !== '') {
        this.model.quantities.push(this.newQuantity);
        newQuantity = {
            price: '',
            name: ''
        };
    }
}

Then you could display the list of quantity objects in a view like this: 然后,您可以在这样的视图中显示数量对象的列表:

<div *ngFor="let quantity of model.quantities; let i=index;" >
    <input type="text" value="{{quantity.name}}" [(ngModel)]="model.quantities[i].name" />
    <input type="text" value="{{quantity.price}}" [(ngModel)]="model.quantities[i].price"  />
</div>

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

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