2017-04-14 2 views
0

typescript를 처음 사용하며 다음 Knockout + js를 knockout + typescript로 변환하려고합니다. 녹아웃 + JS는 .... 그러나 나는 아직도 타이프 라이터와 함께 작동하도록 실패하고, 노력하고 있습니다typescript 및 knockout이있는 선택 상자 모델

보기 :

<select data-bind="options: choices, value: selectedChoice"></select> 

모델 :

var MyModel = { 
    choices: ["Blue", "White", "Black", "Yellow"], 
    selectedChoice: ko.observable("Yellow") 
}; 

MyModel.selectedChoice.subscribe(function(newValue) { 
    alert("the new value is " + newValue); 
}); 


ko.applyBindings(MyModel); 

타이프 :

import BaseVM = require("./BaseVM"); 

class MyModel extends BaseVM { 
    choices = ko.observableArray(["one", "two", "three"]); 

    //Here selectedChoice subscribe in typescript... 

} 

export = MyModel; 

답변

0

클래스 내 typescript에서 구독 코드를 생성자 함수 안에 넣어야합니다. 그런 다음 "this"를 사용하여 구독하려는 속성에 액세스 할 수 있습니다.

class MyModel extends BaseVM { 
    choices = ko.observableArray(["one", "two", "three"]); 
    selectedChoice = ko.observable("Yellow"); 

    constructor() { 
     this.selectedChoice.subscribe(function (newValue) { 
      alert("the new value is " + newValue); 
     }); 
    } 
} 
+0

매력처럼 작동합니다. –