0

我无法将命名空间与淘汰赛验证结合起来。这打破了验证:

myNameSpace = {
    viewModel: {
        name: ko.observable().extend({ digit: { digit: true, message: "digits only"} })
    }
};
ko.validation.init({});
ko.applyBindings(myNameSpace);

​相对于:

myNameSpace = {
    viewModel: {
        name: ko.observable().extend({ digit: { digit: true, message: "digits only"} })
    }
};
ko.validation.init({});
ko.applyBindings(myNameSpace.viewModel);

哪个工作正常。

谁能向我解释其中的区别?​ ​</p>

4

1 回答 1

0

This is about the existence of objects and properties. You might have had your bindings to the "name" property. If you do ko.applyBindings(myNameSpace);, ko looks for a property "name" within myNameSpace object. This does not exist.

When you bind it with ko.applyBindings(myNameSpace.viewModel);, it looks for name within the view model object. It does find it and there fore is able to bind to the property or observable.

In the first case however, if you were to use the data-bind with "value : viewModel.name" it would work, because it would be looking for the name property of the viewModel property of the myNameSpace object.

于 2012-12-05T13:10:14.200 回答