选择框模型与打字稿和淘汰赛

时间:2017-04-14 14:34:10

标签: javascript typescript knockout.js

我是打字稿的新手,并希望将以下Knockout + js转换为knockout + typescript。 Knockout + js正在运行,但是我仍然无法使用typescript ....

查看:

<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;

1 个答案:

答案 0 :(得分:0)

在类中的typescript中,您需要将订阅代码放在构造函数中。然后你就可以使用&#34;这个&#34;访问您要订阅的媒体资源。

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);
        });
    }
}