knockout.js未捕获错误:无法解析绑定

时间:2014-09-11 15:59:44

标签: javascript knockout.js velocity

我正在使用一个来自互联网的简单示例,它不起作用,并且给我解析错误。

Uncaught Error: Unable to parse bindings.
Message: ReferenceError: selectedCategory is not defined;
Bindings value: text: selectedCategory knockout-2.1.0.min.js:49
Uncaught Error: Unable to parse bindings.
Message: ReferenceError: books is not defined;
Bindings value: foreach: books

这是我在速度模板中的总代码:

<html>
<head>
    <script type="text/javascript" src="$application.asset('/js/thirdparty/knockout-2.1.0.min.js')">
    </script>
    <script type="text/javascript">
        function viewModel() {
            var self = this;
            self.books = ko.observableArray([
                { title: 'The Secret', author: 'Rhonda Byrne' },
                { title: 'The Power', author: 'Rhonda Byrne' },
                { title: 'The Magic', author: 'Rhonda Byrne' }
            ]);
        }
        ko.applyBindings(new viewModel());
    </script>

</head>

<table>
    <thead>
    <tr><th>Title</th><th>Author</th></tr>
    </thead>
    <tbody data-bind="foreach: books">
    <tr>
        <td data-bind="text: title"></td>
        <td data-bind="text: author"></td>
    </tr>
    </tbody>
</table>
</html>

任何帮助都会有用。

1 个答案:

答案 0 :(得分:0)

这条线对我来说很可疑:

<script type="text/javascript" src="$application.asset('/js/thirdparty/knockout-2.1.0.min.js')">
</script>

尝试向Knockout添加其他网址。这是相同的脚本标记,但它指向Cloudflare CDN上的Knockout(同样引用了相同的版本号)。

<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/knockout/2.1.0/knockout-min.js">
</script>