KnockoutJs - Pushing to an element in an observable array does not work

I have an object (analysisLogData) that I use to generate a table using KnockoutJS. Here's the viewModel containing this object: function AppViewModel() { var self = this; self.analysisLogData = ko.observableArray(); self.analysisLogTitle = ko.observable("Warnings") self.changeAnalysisLog = function(title) { self.analysisLogTitle(title) } var data = { "Warnings": [ { "number": 3002, "description": "There may be a problem with the device you are using if you use the default profile" }, { "number": 3001, "description": "There may be a problem with the device you are using if you don't use the default profile" } ] , "Errors": [ { "number": 1000, "description": "No networks are loaded" }, { "number": 1002, "description": "No devices are loaded" }] } self.addLog = function (type, content) { self.analysisLogData()[type].push(content); } self.analysisLogData.push(data) } ko.applyBindings(new AppViewModel()); You can see the result here in a JSFiddle: http://jsfiddle.net/etiennenoel/V4r2e/5/ I want to be able to add an error or a warning without losing the warnings or errors already present. I tried to do the following in the `self.addLog` function: self.addLog = function (type, content) { self.analysisLogData()[type].push(content); } but it says that it can't push to an undefined object...

以上就是KnockoutJs - Pushing to an element in an observable array does not work的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏