knockout.js How to access $index in handler function

后端 未结 3 968
温柔的废话
温柔的废话 2020-12-30 01:45

As I understand, $index is available inside a foreach: binding, giving the index of the object... I have a click: binding e.g. c

3条回答
  •  被撕碎了的回忆
    2020-12-30 02:30

    It is easier with ES6 code. In my html I have an array of pages

    
         
         
    
    

    In the view controller class I have the gotoPage method. The first parameter will be the $index of the foreach. Very simple.

    class ViewModel {
        constructor() {
            this.requestedPage = ko.observable(0);
            this.pages = ko.observableArray([0,1,2,3]);
        }
    
        async run() {
            this.message("Running");
        }
    
        gotoPage(pageRequested, event) {
            this.requestedPage(pageRequested);
            this.run();
        }
    }
    

提交回复
热议问题