此模板引擎不支持'if'在其模板内绑定
This template engine does not support the 'if' binding within its templates
在下面的代码中,当我添加'if' to the bind
这是可修复的,因为我只希望它绑定时,它的可见?
感谢<script id="friendsTemplate" type="text/html">
<li>
<input data-bind="value : name" />
<button data-bind="click: remove">Remove</button>
<label><input type="checkbox" data-bind="checked : isOnTwitter" /> is on twitter</label>
<input data-bind="if:isOnTwitter, value:twitterName" />
</li>
</script>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<%-- <script src="Scripts/knockout-2.2.1.js"></script>--%>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="https://raw.github.com/jquery/jquery-tmpl/master/jquery.tmpl.js"></script>
<script src="Scripts/knockout-2.2.1.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>details</h1>
<p>first name: <input data-bind="value: firstName" /></p>
<p>last name: <input data-bind="value: lastName" /></p>
<p>full name: <span data-bind ="text: fullName"></span></p>
<h2>friends</h2>
<ul data-bind="template: {name:'friendsTemplate',foreach:friends}"></ul>
<script id="friendsTemplate" type="text/html">
<li>
<input data-bind="value : name" />
<button data-bind="click: remove">Remove</button>
<label><input type="checkbox" data-bind="checked : isOnTwitter" /> is on twitter</label>
<%-- <input data-bind="value:twitterName,visible: isOnTwitter" />--%>
<input data-bind="if:isOnTwitter, value:twitterName" />
</li>
</script>
<button data-bind="click: addFriend">add friend</button>
</div>
</form>
</body>
</html>
<script type ="text/javascript">
function friend(name) {
return {
name: ko.observable(name),
isOnTwitter: ko.observable(false),
twitterName: ko.observable(),
remove: function () {
viewModel.friends.remove(this);
}
};
}
var viewModel ={
firstName: ko.observable("bert"),
lastName: ko.observable("smith"),
friends: ko.observableArray([new friend('steve'), new friend('annie')]),
addFriend: function () {
this.friends.push(new friend('bob'));
}
};
viewModel.fullName = ko.dependentObservable(function () {
return this.firstName() + " " + this.lastName();
},viewModel);
ko.applyBindings(viewModel);
</script>
if
绑定需要放置在容器上。它只控制子元素的绑定/呈现。
你会想要这样做:
<div data-bind="if: isOnTwitter">
<input data-bind="value: twitterName" />
</div>
或
<!-- ko if: isOnTwitter -->
<input data-bind="value: twitterName" />
<!-- /ko -->
相关文章:
- 如何在数据绑定视图中模板化If Else结构
- KnockoutJS:无法处理绑定"if:function(){return conversations}”;
- KnockoutJS:将if绑定封装在自定义绑定中
- 挖空.js使用 $index 和 if 绑定
- jQuery Mobile:使用 tap 绑定 if 语句
- 在 Foreach 循环中使用 IF 条件来比较数据绑定值
- KnockoutJS “if: ” 绑定无法正常工作
- Knockout Js 的渲染方式与基于 Json 值的 if 数据绑定不同
- 在 if 语句中淘汰奇怪的绑定问题
- Knockoutjs:如何在不使用if的情况下过滤foreach绑定
- 使用ko.mapping插件时,IF绑定未按预期工作
- knockout.js:If和foreach绑定,检查可观察数组上的真值
- 带有if绑定的$index不起作用
- Angularjs动态ng-if数据绑定在span中
- Angular JS中ng-if表达式中条件单向绑定的语法
- 剔除条件绑定(但不是本机的“if”方式)
- 可以在Knockout中创建自定义if绑定
- 我可以在Knockoutjs中的if绑定中访问jquery调用中的$data吗?
- 此模板引擎不支持'if'在其模板内绑定
- 自定义指令在ng-if中第二次绑定不正确