Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ngFor是最后一个元素的唯一设置值_Javascript_Arrays_Angular_Ngfor_Angular Ng If - Fatal编程技术网

Javascript ngFor是最后一个元素的唯一设置值

Javascript ngFor是最后一个元素的唯一设置值,javascript,arrays,angular,ngfor,angular-ng-if,Javascript,Arrays,Angular,Ngfor,Angular Ng If,我有一个名为this.itemsArray的对象数组。数组中的对象包含属性rate。当用户输入值时,使用[(ngModel)]设置速率 每当用户输入费率时,我想显示下面的按钮。如果用户未输入费率,则我想隐藏该按钮 如果数组中只有一个项(因此只有一个速率),则代码可以正常工作。当有多个项(因此数组中有多个速率)时,就会出现问题。在这种情况下,问题是只有当数组中的最后一项(速率)有值时,按钮才被隐藏 当数组中的任何速率为空时,我需要隐藏该按钮。我怎样才能做到这一点 <div *ngFor =

我有一个名为this.itemsArray的对象数组。数组中的对象包含属性
rate
。当用户输入值时,使用
[(ngModel)]
设置
速率

每当用户输入
费率时,我想显示下面的按钮。如果用户未输入
费率
,则我想隐藏该按钮

如果数组中只有一个项(因此只有一个速率),则代码可以正常工作。当有多个项(因此数组中有多个速率)时,就会出现问题。在这种情况下,问题是只有当数组中的最后一项(速率)有值时,按钮才被隐藏

当数组中的任何速率为空时,我需要隐藏该按钮。我怎样才能做到这一点

 <div *ngFor = "let item of this.itemsArray;  let i = index">
                                                                            
     <button  *ngIf = "this.itemsArray[i].rate" >Proceed</button>

</div>


继续

您应该在ts文件中的某个点处理项目(smth like
this.hideButton=this.itemsArray.some(item=>!item.rate)
),然后在模板中继续
您已经在使用单个元素
项目
。为什么要使用
this.itemsArray[i].rate

 <div *ngFor = "let item of this.itemsArray;  let i = index">
                                                                            
   <button  *ngIf = "item && item.rate" >Proceed</button> // Replace this.itemsArray[i] with item

</div>

// the condition item && item.rate ==> it will check if the item is there and item.rate is there. If its not there, it wont display that element in the DOM


继续//将此.itemsArray[i]替换为项目
//条件item&&item.rate==>将检查item是否存在以及item.rate是否存在。如果它不在那里,它就不会在DOM中显示该元素

不要使用
让i=index
,使用
索引作为i
。此外,不需要使用索引;您只需参考
您使用的是哪个角度版本?我将
let I=index
改为
index as I
。我还将
this.itemsArray[I].rate
更改为
item.rate
,但仍然存在相同的问题。我正在使用
Angular:10.1.0
预期的行为有点不清楚。假设数组中有两个项目,其中只有一个设置了速率。是否要显示该项目的按钮而不是另一个项目的按钮?我将很快尝试此操作并返回给您。谢谢