【问题标题】:Selection box model with typescript and knockout带有打字稿和敲除的选择框模型
【发布时间】:2017-09-10 20:05:46
【问题描述】:

我是 typescript 的新手,想将以下 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;

【问题讨论】:

    标签: javascript typescript knockout.js


    【解决方案1】:

    在类中的打字稿中,您需要将订阅代码放入构造函数中。然后你可以使用“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);
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      • 2016-12-31
      • 1970-01-01
      • 2015-11-28
      • 2022-01-01
      • 1970-01-01
      相关资源
      最近更新 更多