Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么viewmodel函数代码在加载时执行?_Javascript_Knockout.js_Dom Events - Fatal编程技术网

Javascript 为什么viewmodel函数代码在加载时执行?

Javascript 为什么viewmodel函数代码在加载时执行?,javascript,knockout.js,dom-events,Javascript,Knockout.js,Dom Events,我的viewmodel中有一个函数,在页面加载时立即执行 我有一个按钮,可以简单地显示警报(目前): 查看如何在加载时显示警报? 你知道为什么会这样吗?这里有两种解决问题的方法: 将您的函数包装到另一个函数中: <button data-bind="click: function () { $root.showData('user'); }">POS User</button> 作为一个有点无关的评论,你有没有想过你的缩写词“POS”以及它对很多人的完整含义 LOL-

我的viewmodel中有一个函数,在页面加载时立即执行

我有一个按钮,可以简单地显示警报(目前):

查看如何在加载时显示警报?


你知道为什么会这样吗?

这里有两种解决问题的方法:

将您的函数包装到另一个函数中

<button data-bind="click: function () { $root.showData('user'); }">POS User</button>

作为一个有点无关的评论,你有没有想过你的缩写词“POS”以及它对很多人的完整含义

LOL-销售点。事实上,直到你提到这件事,我才想起,但当我读到你的评论时,我确实笑了谢谢你对我的位置的帮助。哈哈!有益健康:)

let User = function(id, firstName, lastName, email, phone, isActive ){
  this.id = id;
  this.firstName = firstName;
  this.lastName = lastName;
  this.email = email;
  this.phone = phone;
  this.isActive = isActive;
}


function UsersViewModel () {

    var self = this; // Scope Trick

    // User, Customer, Tech
    self.currentUserType = 
    ko.observable("posUser");

    // Arrays
    self.posUsers    = ko.observableArray();
    self.customers   = ko.observableArray();
    self.technicians = ko.observableArray();

    self.currentUsers = ko.observableArray();


    self.currentUsers.push(new User(12,"John","Smith","john@gmail.com","800-333-3333",true));


    self.showData = function(userType){
        alert(userType);
    }
};


ko.applyBindings(new UsersViewModel());

<button data-bind="click: function () { $root.showData('user'); }">POS User</button>
<!-- ko with: user -->    
<button data-bind="click: $root.showData">POS User</button>
<!-- /ko -->

self.showData = function (user) { ... };