我在Knockout.js中收到此错误:
未捕获的TypeError:无法从JS'中读取属性'未定义的
我是Knockout JS的新手。我正在开发Oracle内容和体验云中的Knockout JS。我的方案是我尝试调用Rest API并获取结果并将其显示在表中。
我在REST API网址下面使用我的应用程序进行测试 http://learn.knockoutjs.com/mail?folder=inbox
以下是我的代码:
/* globals define */
define(['knockout', 'jquery', 'text!./knockout.mapping-latest.js', 'text!./mailbox.json', 'css!./css/design.css'], function (ko, $, mapping, css) {
'use strict';
// ----------------------------------------------
// Define a Knockout Template for your component
// ----------------------------------------------
var sampleComponentTemplate =
'<div>' +
'<p><input data-bind="value: searchValue" class="box"/></p>' +
'<button data-bind="click: getCustomers">Knock out Search</button>' +
'</div>' +
'<table>' +
'<thead>' +
'<tr>' +
'<th>From</th>' +
'<th>To</th>' +
'<th>Subject</th>' +
'<th>Date</th>' +
'</tr>' +
'</thead>' +
'<tbody data-bind="foreach: mails">' +
'<tr>' +
'<td data-bind="text: from"></td>' +
'<td data-bind="text: to"></td>' +
'<td data-bind="text: subject"></td>' +
'<td data-bind="text: date"></td>' +
'</tr>' +
'</tbody>' +
'</table>';
// ----------------------------------------------
// Define a Knockout ViewModel for your template
// ----------------------------------------------
var SampleComponentViewModel = function (args) {
this.searchValue = ko.observable("Hi");
this.mails = ko.observableArray();
this.getCustomers = function () {
alert("Inside get customers ");
$.ajax({
type: 'GET',
crossDomain: true,
url: 'documents/folder/F49A137E34CB4B6DFD302FB90A04F4D8CA1E8A3D5B3E/_assets/mailbox.json',
data: JSON.stringify(this.mails),
success: function(data) {
var observableData = ko.mapping.fromJS(data);
var array = observableData();
this.mails(array);
},
error:function(jq, st, error){
alert("Inside Error Method " + error + " jq is " + jq + "st is " + st);
}
});
};
};
答案 0 :(得分:0)
我通过初始化self = this解决了这个问题,并在代码中用self替换了