2017-09-14 27 views
1

這個問題花了我一天調試它,但仍沒有運氣。爲什麼我的數組仍然是空的,即使我已經在控制器中爲這個數組賦值了? (我用angularjs,沒有棱角)

問題:this.gridOptions.data = this.allTemplatesFromClassificationRepo ; ** this.allTemplatesFromClassificationRepo **仍然是一個空數組。在這行代碼執行之前,我已經調用activate()函數將數組賦給** this.allTemplatesFromClassificationRepo **。請參閱此行this.allTemplatesFromClassificationRepo = templateReorderProperties;

附:雖然我無法在控制器中獲得allTemplatesFromClassificationRepo的值,但我可以在html中獲得它的值。

namespace app.admin { 
'use strict'; 

class FooController { 
    static $inject: Array<string> = []; 

    constructor() { 
     this.activate(); 
    } 

    activate() { 
     this.getData(); 

     this.classificationFoo = this.data[0]; 

     this.getTemplateFromGivenRepo(this.classificationFoo.id, this.classificationFoo.displayName); 

     this.populateData(); 
    } 

    data: any; 

    classificationFoo: any; 

    allDataFromclassificationFoo: any = []; 

    // demo grid 
    gridOptions = { 
     enableFiltering: true, 
     }, 
     data: [] 
    }; 

    populateData() { 
      this.gridOptions.data = this.allTemplatesFromClassificationRepo ; 
    } 

    getData() { 
     this.fooService.getUserData(); 
     this.data = this.fooService.userdata; 
    } 

    getTemplateFromGivenRepo(fooId: string, fooName: string) { 
     switch (fooId) { 
      case 'FOO': 
       this.TemplateApi.templatesAvaiableForRepoIdGET(fooId).then(data => { 
        data.forEach(element => { 
         element.fooName = fooName; 
        }); 

        let templateReorderProperties = data 

        this.allTemplatesFromClassificationRepo = templateReorderProperties; 
       }, error => { 

       }); 
       break; 

      default: 
       break; 
     } 
    }; 
} 

class Bar implements ng.IDirective { 
    static $inject: Array<string> = []; 

    constructor() { 
    } 

    bindToController: boolean = true; 
    controller = FooController; 
    controllerAs: string = 'vm'; 
    templateUrl: string = 'app/foo.html'; 

    static instance(): ng.IDirective { 
     return new Bar(); 
    } 
} 

angular 
    .module('app.admin') 
    .directive('bar', Bar.instance); 

}

+0

我懷疑函數中'this this'this.allTemplatesFromClassificationRepo'寫的是與你期望的不同的'this'。 (@see:https://stackoverflow.com/questions/36489579/this-within-es6-class-method) – birdspider

+0

@birdspider有我的解決方案嗎?我不知道如何解決它,我已經盡了全力 – EntryLeveDeveloper

+0

爲了檢驗這一理論對「這個」嘗試略高於切換(方法的第1行)和寫'讓_this = this',當你指定數組做'_this 。 allTemplatesFromClassificationRepo = templateReorderProperties' – cYrixmorten

回答

2

getTemplateFromGivenRepo是異步操作。

移動this.populateGridData();調用內部getTemplateFromGivenRepo

this.allTemplatesFromClassificationRepo = templateReorderProperties;

getTemplateFromGivenRepo(repoId: string, repoName: string) { 
      switch (repoId) { 
       case 'CLASSIFICATION': 
        this.TemplateApi.templatesAvaiableForRepoIdGET(repoId).then(data => { 

         this.allTemplatesFromClassificationRepo = templateReorderProperties; 
         this.populateGridData(); // call here 
        }, error => { 

        }); 
      } 
     }; 

OR

您可以從getTemplateFromGivenRepo回報的承諾,在then能夠success回調,調用 this.populateGridData();

+0

哇。你的第一個解決方案很好。你節省了我的一天。非常感謝。您能否給我一些關於您的解決方案的教程或文章? (異步操作) – EntryLeveDeveloper

+0

https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323這將超過所有可能的場景 –

+0

謝謝。我正在查看你發給我的鏈接。 – EntryLeveDeveloper

0

我認爲這個問題是在this情況下,不同的內部承諾的決心。

嘗試在開始像寫:

var self = this; 

,並且更改後的所有thisself關鍵。

a.e:

self.gridOptions.data = self.allTemplatesFromClassificationRepo; 

// and so on ... 

通過這種方式,您將保證您使用相同的範圍實例


希望它會工作

+0

你能讓我知道我應該在哪裏放'let self = this'在我的代碼? – EntryLeveDeveloper

+0

第一排在構造函數實現'構造(私人repoEventService, 私人TemplateApi, 私人$過濾器){風險自=這一點; self.activate(); ... }' –

+0

我嘗試了你的建議,但是我收到了這樣的錯誤:'Windows'上不存在'TS2339,屬性'xxx'' – EntryLeveDeveloper

相關問題