带打字稿和剔除的选择框模型 [英] Selection box model with typescript and knockout

查看:51
本文介绍了带打字稿和剔除的选择框模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to typescript and will like to convert the following Knockout+js to knockout+typescript. The Knockout+js is working, however I am still failing to make it work with 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;

推荐答案

在类中的打字稿中,您需要将订阅代码放入构造函数中.然后,您可以使用此"来访问您要订阅的属性.

In typescript from within the class you'll need to put your subscription code inside of a constructor function. Then you can use "this" to access the property you want to subscribe to.

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

这篇关于带打字稿和剔除的选择框模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆