1

这是我的 js 文件:

function AppViewModel() 
{
    this.firstName = ko.observable("Bert");
    this.lastName = ko.observable("Bertington");
}
ko.applyBindings(AppViewModel());

这是我的 html 文件:

<html>
<head>
    <script type='text/javascript' src='ko.js'></script>
    <script type='text/javascript' src='knockout-2.1.0.debug.js'></script>

</head>
<body>
    <p>First name: <strong data-bind="text: firstName"></strong></p>
    <p>Last name: <strong data-bind="text: lastName"></strong></p>

    <p>First name: <input data-bind="value: firstName" /></p>
    <p>Last name: <input data-bind="value: lastName" /></p>
</body>
</html>

未捕获的 ReferenceError: ko is not defined

有人可以帮我弄这个吗 ?

提前致谢

4

3 回答 3

8

尝试颠倒您的 JavaScript 文件的顺序 -

<script type='text/javascript' src='knockout-2.1.0.debug.js'></script>
<script type='text/javascript' src='ko.js'></script>

我怀疑该文件在主“敲除”js文件中定义之前ko.js试图访问该对象。ko

于 2012-07-13T08:29:12.480 回答
0

对我来说,解决方案是将我的 ko 脚本按上述顺序排列,并在使用 JQuery(或等效)文档就绪功能加载文档后执行任何 ko.xyz 代码。例如

 $(document).ready(function(){
            var model = new ReviewersViewModel();
            ko.applyBindings(model);

            $('.datetimepicker_mask').datetimepicker({
                mask: '9999/19/39 29:59'
            });

            ko.bindingHandlers.datetimepicker = {
                init: function (element, valueAccessor, allBindingsAccessor) {
                    //initialize datepicker with some optional options
                    var options = allBindingsAccessor().datetimepickerOptions || {};
                    $(element).datetimepicker(options);

                    //handle the field changing
                    ko.utils.registerEventHandler(element, "change", function () {
                        var observable = valueAccessor();
                        try {
                            observable($(element).datetimepicker("getDate"));//****
                        }
                        catch (ex) { }
                    });

                    //handle disposal (if KO removes by the template binding)
                    ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
                        $(element).datetimepicker("destroy");
                    });

                },
                update: function (element, valueAccessor) {
                    var value = ko.utils.unwrapObservable(valueAccessor()),
                        current = $(element).datetimepicker("getDate");

                    if (value - current !== 0) {
                        $(element).datetimepicker("setDate", value);
                    }
                }
            };
        });
于 2014-04-01T12:36:05.057 回答
0

定义脚本,它是 Knockout js 库,它包含淘汰预定义的方法。

于 2017-02-18T11:54:58.297 回答