Hi I’m trying to do a foreach using a observableArray and I’m getting:
Uncaught Error: Unable to parse bindings.
Message: ReferenceError: Nome is not defined;
Bindings value: text: Nome
Here’s my HTML:
<table data-bind="foreach: funcionarios">
<tr>
<p>Nome: <span data-bind="text: Nome"></span></p>
<p>Apelido: <span data-bind="text: Apelido"></span></p>
<p>Hobby: <span data-bind="text: Hobby"></span></p>
<p>Função: <span data-bind="text: Funcao"></span></p>
<p>Ramal: <span data-bind="text: Ramal"></span></p>
<p>Email: <span data-bind="text: Email"></span></p>
<p>Celular: <span data-bind="text: Celular"></span></p>
<p>Aniversário: <span data-bind="text: Aniversario"></span></p>
</tr>
</table>
Here’s my Js:
var FuncionarioViewModel = function () {
var me = this;
function Funcionario(base, id, nome, apelido, hobby, funcao, ramal, email, celular, aniversario) {
var me = this;
me.Id = id;
me.Nome = ko.observable(nome);
me.Apelido = ko.observable(apelido);
me.Hobby = ko.observable(hobby);
me.Funcao = ko.observable(funcao);
me.Ramal = ko.observable(ramal);
me.Email = ko.observable(email);
me.Celular = ko.observable(celular);
me.Aniversario = ko.observable(aniversario);
};
me.funcionarios = ko.observableArray([]);
me.add = function (id, nome, apelido, hobby, funcao, ramal, email, celular, aniversario) {
me.funcionarios.push(new Funcionario(me, id, nome, apelido, hobby, funcao, ramal, email, celular, aniversario));
};
}
var viewModel = new FuncionarioViewModel();
ko.applyBindings(viewModel);
And here’s the Fiddle link: http://jsfiddle.net/Lrqeb/8/
I’m not want to load page with default data in that array.
EDIT:
Ok, just checked your jsfiddle. I updated to use the
ifbinding to check if the array contains any elements before trying theforeachwhich will stop the binding error:http://jsfiddle.net/Lrqeb/6/
I wrapped your
foreachwith the following:EDIT2:
Also, you’ve applied the
foreachto the table tag which won’t work. I changed it to use the tbody tag instead and that works fine here: http://jsfiddle.net/Lrqeb/10/The changes look like this: