简体   繁体   English

当我绑定到一个数组时,它似乎没有正确反映到标记 ngif

[英]When i bind to a array, it doesnt seem to properly reflect to the markup ngif

I have an @Input which I assign a value in the parent component.我有一个@Input ,我在父组件中分配了一个值。 I assign the value from an empty array to a array of size one.我将空数组中的值分配给大小为 1 的数组。

In this component though, it doesnt seem to properly reflect this changed array in the form of an NGIF.但是在这个组件中,它似乎没有以 NGIF 的形式正确反映这个改变的数组。 It is pretty simple, and I didnt think I needed to use setters as it is taking the object as is etc.这很简单,我认为我不需要使用 setter,因为它按原样处理对象等。

Below is the explicit code I am using for the implementation下面是我用于实现的显式代码

Parent Item A Markup父项 A 标记

<input-datalist [buttons]="buttons"></input-datalist>

Parent Item A Code:父项 A 代码:

buttons: any[];
ngOnInit() { this.buttons = [ {key: "value"} ];

Component input-datalist Markup:组件input-datalist标记:

<jqxGrid (contextMenu)="handleContext($event)"></jqxGrid>
<jqxMenu *ngIf="buttons && buttons.length > 0">
  <ul>
    <li *ngFor="let btn of buttons">{{btn.key}}</li>
  </ul>
</jqxMenu>

Component input-datalist Code:组件input-datalist代码:

@Input() buttons: any[]

@ViewChild('gridMenu', { read: null, static: true }) gridMenu: jqxMenuComponent;
handleContext(event){
    let scrollTop = window.scrollY;
    let scrollLeft = window.scrollX;
    console.log("Does GridMenu Exist? ", this.gridMenu, 'Button Count: ', this.buttons)
    this.gridMenu.open(parseInt(event.clientX) + 5 + scrollLeft, parseInt(event.clientY) + 5 + scrollTop);
    event.preventDefaults()
}

When I do this, right clicking will call the context menu, but it will say: open does not exist for gridMenu.当我这样做时,右键单击将调用上下文菜单,但它会说:gridMenu 不存在 open。 I took off the ngIf, to see if the issue is with the MENU itself, but it seems that when I set the buttons properly, it still thinks it is empty, even though when I bring it with the console, it shows an array of size one.我取下 ngIf,看看问题是否出在 MENU 本身上,但似乎当我正确设置按钮时,它仍然认为它是空的,即使当我把它带上控制台时,它显示了一个数组一号。

Is there something I am missing when it comes to setting the property?在设置属性时,我有什么遗漏吗? It follows every other implementation I have seen.它遵循我见过的所有其他实现。

i think the problem has nothing to do with inputs.我认为问题与输入无关。 it causes from this line它从这一行引起

@ViewChild('gridMenu', { read: null, static: true }) gridMenu: jqxMenuComponent;

here static: true causes ViewChild query not to wait for *ngIf evaluated.这里static: true导致ViewChild查询不等待*ngIf评估。 in other words static: true causes ViewChild to execute before *ngIf is evaluated ( details here more details here ).换句话说, static: true导致ViewChild在评估*ngIf之前执行(详情请点击此处,更多详细信息请点击此处)。 So changing as follows should fix your problem.因此,如下更改应该可以解决您的问题。

@ViewChild('gridMenu', { static: false }) gridMenu: jqxMenuComponent;

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

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