选择框模型与打字稿和淘汰赛(Selection box model with typescript and knockout)
我是新来的打字稿,并希望将下面的Knockout + js转换为knockout +打字稿。 Knockout + js正在工作,但是我仍然无法使它与打字稿一起工作....
视图:
<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;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....
View:
<select data-bind="options: choices, value: selectedChoice"></select>Model:
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);Typescript:
import BaseVM = require("./BaseVM"); class MyModel extends BaseVM { choices = ko.observableArray(["one", "two", "three"]); //Here selectedChoice subscribe in typescript... } export = MyModel;最满意答案
在类内的打字稿中,您需要将您的订阅代码放入构造函数中。 然后你可以使用“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); }); } }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); }); } }更多推荐
MyModel,selectedChoice,choices,typescript,电脑培训,计算机培训,IT培训"/> <met
发布评论