Knockout observableArray in TypeScript

后端 未结 2 1422
春和景丽
春和景丽 2021-02-15 16:14

What is the proper way to initialize a Knockout observableArray in a TypeScript class?

I am doing something like this:

   module ViewModel { 

        de         


        
2条回答
  •  猫巷女王i
    2021-02-15 16:56

    I just did this in TypeScript 1.3 (though it should work in older versions too):

    pendingAddAssociations: KnockoutObservableArray = ko.observableArray([]);
    

    for your example, if you have an Agency class.

    export class IndexPageViewModel {
    
            agencies: KnockoutObservableArray;
    
            constructor () {
                this.agencies = ko.observableArray([]);
            }
        }
    

    I've also found that you can cast to any this.agencies = []; This is useful if you are using the Knockout ES5 plugin.

提交回复
热议问题