简体   繁体   English

setRowData在首次调用Angular 6上不起作用

[英]setRowData Not Work on First Call Angular 6

I have an ag-grid that has buttons for changing the data of each row. 我有一个ag-grid,它具有用于更改每一行数据的按钮。 When I click the button, it supposed to show another ag-grid within a bootstrap modal; 当我单击按钮时,它应该在引导模态中显示另一个ag-grid。 But the problem is when the grid loads for the first time, it does not show any data as it said "No Rows To Show". 但是问题是,当网格第一次加载时,它没有显示任何数据,因为它说“没有要显示的行”。 And when I click for the second time it shows grid data. 当我第二次单击时,它将显示网格数据。

Here is my code: 这是我的代码:

OnGridReady(params){
      this.gridApi = params.api;
      this.loadQuestions();
      this.gridApi.setRowData(this.Questions);
     }

As you want to know more about loadQuestion() 如您想进一步了解loadQuestion()

private loadQuestion(){
    this.managementService.getQuestions().subscribe(
        (_q: Question[]) => {
             this.Questions = _q;
        },(err){ blah blah }    
    );

Can you tell where is the problem? 你能告诉问题出在哪里吗?

Do setRowData inside your loadQuestion . setRowData内部执行loadQuestion

private loadQuestion(): void => {
   this.managementService.getQuestions().subscribe(
      (_q: Question[]) => {
         this.Questions = _q;
          this.gridApi.setRowData(this.Questions);
      },(err){ blah blah }    
);

The issue I think you got was due to when you did setRowData , data for this.Questions was not available. 我觉得你有这个问题的原因是,当你做了setRowData ,数据this.Questions不可用。

Next time when you loaded, it would be there. 下次加载时,它将在那里。


Update 更新

When you define the function as per what is there in the question, you may face the below error. 当您根据问题中的内容定义函数时,可能会遇到以下错误。

"Cannot read property 'setRowData' of undefined". “无法读取未定义的属性'setRowData'”。

You can use arrow function to get the outer scope available inside your function as per the code shown in this answer. 您可以根据此答案中显示的代码使用箭头函数获取函数内部可用的外部范围。

Refer the link for more details on the Arrow function 请参阅链接以获取有关箭头功能的更多详细信息

Lovingly called the fat arrow (because -> is a thin arrow and => is a fat arrow) and also called a lambda function (because of other languages). 亲切地称为粗箭头(因为->是细箭头,而=>是粗箭头),也被称为lambda函数(由于其他语言)。 Another commonly used feature is the fat arrow function ()=>something . 另一个常用功能是胖箭头功能()=>something The motivation for a fat arrow is: You don't need to 粗箭头的动机是:您不需要

  1. Keep typing function 保持打字功能
  2. It lexically captures the meaning of this 它从字面上捕获了这个的含义
  3. It lexically captures the meaning of arguments 它从字面上捕获了参数的含义

As I investigate further in Angular, I found that this type of calling the service is Async and it's normal to show that "Cannot read property 'setRowData'" because data is not loaded completely. 当我在Angular中进行进一步调查时,我发现这种类型的服务调用是Async,通常会显示“无法读取属性'setRowData'”,因为数据没有完全加载。 I just do this: 我只是这样做:

private loadQuestion() {
    this.Questions = [];
    this.managementService.getQuestions().subscribe(
         _q => {
             _q.forEach(element => {
                 this.Questions.push(element);
             });
             this.gridApi.setRowData(this.Questions);
         },(err){ blah blah }    
);

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

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