Knockout.js 找不到 ID 错误的模板
救命,我被困住了! 我正在尝试在 knockout.js 中执行一项简单的任务。基本上,我想让一个项目数组在表中生成一系列行。我正在使用 jquery 和 jquery.tmpl.js。我以前曾多次这样做过,但由于某种原因它不起作用。这是我的代码。
ASPX 页面
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" ClientIDMode="Static" Inherits="EditableGridPrototype._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script src="Styles/jquery-1.6.4.min.js" type="text/javascript"></script>
<script src="Styles/jquery.tmpl.js" type="text/javascript"></script>
<script src="Styles/knockout-1.2.1.debug.js" type="text/javascript"></script>
<script src="Styles/knockout.mapping.js" type="text/javascript"></script>
<script src="Scripts/jquery.json-2.2.min.js" type="text/javascript"></script>
<script src="Grid.js" type="text/javascript"></script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h3>Transactions</h3>
<input type="checkbox" data-bind="checked: canSelect" />
<span data-bind="text: existingTransactions().length" />
<table width="99%" style="margin-top: 10px" data-bind='template: "existingTransactionsTemplate"'>
<script type='text/html' id='existingTransactionsTemplate'>
{{each(i, tran) existingTransactions()}}
<tr><td>hello</td></tr>
{{/each}}
</script>
</table>
</asp:Content>
复选框和跨度的绑定起作用。该复选框被选中,并且 2 被写入页面。
这是我的 js Grid.js 文件
$(document).ready(function () {
var transactionsViewModel = {
canSelect: ko.observable(true),
existingTransactions: ko.observableArray([
{ canSelect: true, amount: 100 },
{ canSelect: false, amount: 200}])
};
ko.applyBindings(transactionsViewModel);
});
使用的模板很简单,我希望它首先显示行,所以我知道这一切都有效。是的,我不小心把js文件放在Styles文件夹中,这只是一个原型。 :)
感谢您的帮助!!
Help, I'm stuck!
I'm trying to do a simple task in knockout.js. Basically, I'd like to have an array of items generate a series of rows in a table. I'm using jquery, and jquery.tmpl.js. I've done this before many times, but for some reason it is not working. Here's my code.
ASPX Page
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" ClientIDMode="Static" Inherits="EditableGridPrototype._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script src="Styles/jquery-1.6.4.min.js" type="text/javascript"></script>
<script src="Styles/jquery.tmpl.js" type="text/javascript"></script>
<script src="Styles/knockout-1.2.1.debug.js" type="text/javascript"></script>
<script src="Styles/knockout.mapping.js" type="text/javascript"></script>
<script src="Scripts/jquery.json-2.2.min.js" type="text/javascript"></script>
<script src="Grid.js" type="text/javascript"></script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h3>Transactions</h3>
<input type="checkbox" data-bind="checked: canSelect" />
<span data-bind="text: existingTransactions().length" />
<table width="99%" style="margin-top: 10px" data-bind='template: "existingTransactionsTemplate"'>
<script type='text/html' id='existingTransactionsTemplate'>
{{each(i, tran) existingTransactions()}}
<tr><td>hello</td></tr>
{{/each}}
</script>
</table>
</asp:Content>
The bindings for the checkbox and span do work. The checkbox is checked, and 2 is written out to the page.
Here's my js Grid.js file
$(document).ready(function () {
var transactionsViewModel = {
canSelect: ko.observable(true),
existingTransactions: ko.observableArray([
{ canSelect: true, amount: 100 },
{ canSelect: false, amount: 200}])
};
ko.applyBindings(transactionsViewModel);
});
The template that is used is trivial, I'd like it to just display rows first, so I know it all works. And yes, I put the js files in the Styles folder by accident, this is just a prototype. :)
Thanks for your help!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其更改
为:
就可以了。
Change this:
to:
and you should be good to go.