简体   繁体   English

剔除简单绑定不起作用

[英]knockout simple binding not working

I am trying a simple example for binding an objects' attribute to a UI. 我正在尝试将对象的属性绑定到UI的简单示例。

My example is here http://jsfiddle.net/arendu/m14mohda/17/ 我的示例在这里http://jsfiddle.net/arendu/m14mohda/17/

I have the following HTML: 我有以下HTML:

The name is <span data-bind="text: personName"></span>
<button type="button" onlick="changeName()">Click Me!</button>

And the following js script: 和以下js脚本:

var myViewModel = {
    personName: ko.observable('Foo'),
    personAge: ko.observable(123)
};

var changeName = function () {
    myViewModel.personName("Bar")
}
ko.applyBindings(myViewModel);

My question is why does the displayed name not change from 'Foo' to 'Bar' when the button is clicked? 我的问题是为什么单击按钮时显示的名称不会从“ Foo”更改为“ Bar”?

Thanks 谢谢

This myViewModel.personName = 'Bar' is not going to work, since personName is an observable. myViewModel.personName = 'Bar'不起作用,因为personName是可观察的。 When we want to get the observable's value, we simple call it as a function 当我们想要获得可观察值时,我们简单地将其称为函数

 myViewModel.personName()

While, when we want to set it's value, we call it like above but passing the value as a parameter. 同时,当我们想要设置它的值时,我们像上面一样调用它,但是将值作为参数传递。

 myViewModel.personName("Bar")

You can think it as a getter and a setter. 您可以将其视为吸气剂和吸气剂。

 var myViewModel = { personName: ko.observable('Foo'), personAge: ko.observable(123) }; myViewModel.changeName = function () { myViewModel.personName('Bar'); } ko.applyBindings(myViewModel); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> The name is <span data-bind="text: personName"></span> <button type="button" data-bind="click: changeName">Click Me!</button> 

There were a few problems with your code. 您的代码有一些问题。 The primary issue was in the way you were trying to set an observable. 主要问题在于您尝试设置可观察对象的方式。 Observables should be used as getter/setter functions. Observables应该用作getter / setter函数。

http://jsfiddle.net/m14mohda/16/ http://jsfiddle.net/m14mohda/16/

The name is <span data-bind="text: personName"></span>
<button type="button" data-bind="click: changeName">Click Me!</button>

var myViewModel = {
    personName: ko.observable('Foo'),
    personAge: ko.observable(123)
};

myViewModel.changeName = function() {
    var newName = myViewModel.personName() === 'Foo' ? 'Bar' : 'Foo';
    myViewModel.personName(newName);
}
ko.applyBindings(myViewModel);

First: You never use inline event handlers with knockout. 第一: 切勿将内联事件处理程序与剔除一起使用。 Period. 期。 Everything is done through bindings. 一切都是通过绑定完成的。

Second: You should use classes as viewmodels, it will make your life easier. 第二:您应该将类用作视图模型,这将使​​您的生活更轻松。

 function Person(name, age) { this.name = ko.observable(name), this.age = ko.observable(age) } Person.prototype.changeName = function () { this.name('Bar'); }; ko.applyBindings( new Person('Foo', 123) ); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> The name is <span data-bind="text: name"></span> <button data-bind="click: changeName">Click Me!</button> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM