How to access property on object in viewmodel from html code?

后端 未结 2 719
萌比男神i
萌比男神i 2021-01-26 16:58

I hava a viewmodel as follow :

define(
    [\'jquery\', \'knockout\', \'knockout.mapping\', \'data/data\', \'models/models\'],
    function ($, ko, mapping, data         


        
2条回答
  •  [愿得一人]
    2021-01-26 17:28

    Not sure when that you are applying bindings, but it appears that at the time of binding, you are just binding against the empty object. Then, when your AJAX request finishes, it adds the observables, but since post itself isn't observable, the UI does not update.

    You could consider calling applyBindings after your request finishes like:

    var vm = {
        post: ko.observable()
    };    
    
    //simulate AJAX
    setTimeout(function() {
        vm.post(ko.mapping.fromJS({ title: "hello" }));    
    }, 500);
    
    ko.applyBindings(vm);
    

    Then bind against it like:

    ​​​​​​​​​

提交回复
热议问题