two way binding not working in knockout.js

后端 未结 2 1263
眼角桃花
眼角桃花 2021-01-26 16:36

I am trying to update the UI after chages to viewModel but it doesn\'t work , am I missing something ? http://jsfiddle.net/rdZjb/1/

  viewModel = {
        first         


        
相关标签:
2条回答
  • 2021-01-26 16:39

    You are working with observables in wrong way. Each observable is a function so when you setting or geting value you should use ():

            viewModel.firstName("Paul");
            alert(viewModel.firstName());
    

    Also it is bad practice to use jQuery click event. Use knockout click binding instead:

    <input type="button" value ="click" id="button1" data-bind="click: OnClick"/>
    
    viewModel = {
        firstName: ko.observable("adrian"),
        OnClick: function() {
            this.firstName("Paul");
            alert(this.firstName());
        }
    };
    

    Here is working fiddle: http://jsfiddle.net/vyshniakov/rdZjb/2/

    0 讨论(0)
  • 2021-01-26 16:46

    To change an observable (after initializing it) via Javascript, you need to call it as a function such as

    viewModel.firstName("Paul");
    
    0 讨论(0)
提交回复
热议问题