简体   繁体   English

如何检查Angular中另一个可观察元素是否存在?

[英]How can I check if an element from one observable exists in another in Angular?

I have two list elements side by side. 我有两个并排的列表元素。

NEW      |     APPROVED
-------------------------
ONE   [X]      ONE
TWO   [ ]      THREE
THREE [X]

I have a checkbox that when clicked will disable the checkbox so I can't click it again, and add a class to the parent list element. 我有一个复选框,当单击该复选框时将禁用该复选框,因此我无法再次单击它,并将一个类添加到父级列表元素。 That element then shows on the "APPROVED" list. 然后,该元素显示在“已批准”列表中。

All of this is working great as a user is actually using the application. 当用户实际上正在使用该应用程序时,所有这些都很好用。

When the page loads, the list(s) are populated correctly, but I'm not sure how to apply the class/disable the checkbox to the elements in the "NEW" list. 页面加载时,将正确填充列表,但是我不确定如何将类/禁用复选框应用于“新建”列表中的元素。

When a user clicks the checkbox, I am simply setting a variable in my .ts file: 当用户单击复选框时,我只是在.ts文件中设置了一个变量:

constructor() {
    isApproved: boolean = false;
    ...
}

...

// item has been clicked and status updated into db.
item.isApproved = true;

...
do other stuff

Here is what my html looks like: 这是我的html外观:

<ion-item *ngFor="let item of items" [ngClass]="{approved: item.isApproved}">
    ...
</ion-item>

In my constructor, I am not sure how to check what items are in the "APPROVED" list when the page loads and then apply a class to my "NEW" list. 在我的构造函数中,我不确定如何在页面加载时检查“ APPROVED”列表中的哪些项目,然后将一个类应用于“ NEW”列表。 I know that Items in the "APPROVED" list are showing correctly, those are the items that need to have the class applied to them in the "NEW" column. 我知道“已批准”列表中的项目正确显示,这些是需要在“新”列中将类应用到它们的项目。

In really, really simple js it would be something like: 在非常非常简单的js中,它类似于:

foreach(item In New list as item) {
   if (item is in approved list) {
        item.isApproved = true;
   }
}

Thank you for any suggestions! 感谢您的任何建议!

write the following code snippet in the constructor or ngOnInit method to set the isApproved field: 在构造函数或ngOnInit方法中编写以下代码段以设置isApproved字段:

NewList.forEach(item=>{
    approvedList.forEach(approvedItem=>{
        if(item.text===approvedItem.text){
            item.isApproved = true;
            }
        });
    });
}

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

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