问题
I get the following error when running my knockout code: Uncaught Error: Unable to parse bindings. Message: ReferenceError: measures is not defined; Bindings value: template: { name: 'measureDispTmpl', foreach: measures, as: 'food' }
You can see the jsfiddle code here:
http://jsfiddle.net/nickbuus/eUufc/
When I click the + icon I want the addMeasure method to be called - so that I can add a new measure to the current foodItem and save it. But when I try to call the addMeasure then the method:
self.addMeasure = function (myItem)
is never called and I get the above error.
回答1:
I updated your fiddle. In your code you add a mesureItem in the foodList of the viewModel. That's why the food template doesn't work (because it tries to show an measure and a measure doesn't an measures property).
self.addMeasure = function (myItem) {
var foodIte = self.selectedItem();
var newItem = new MeasureItem();
// mine
myItem.measures.push(newItem);
self.selectedItem(newItem);
// your :
//self.list.push(newItem);
};
See fiddle
In response to your question :
Just add this at end of the addMeasure function:
self.selectedItem(newItem);
I hope it helps.
来源:https://stackoverflow.com/questions/16978731/knockout-error-uncaught-error-unable-to-parse-bindings