Using conditional knockout templates with IE8

时光怂恿深爱的人放手 提交于 2019-12-01 14:06:03

问题


In all 'modern' browsers the following works but not in IE8:

<!-- ko template: {if: $root.itemToEdit.SomeObject() === $data, name: 'EditItemTemplate', afterRender: $root.initializeEditPanel } -->
<!-- /ko -->

I get the following error:

SCRIPT5022: Unable to parse bindings. Message: SyntaxError: Expected identifier, string or number; Bindings value: template: {if: $root.itemToEdit.SomeObject() === $data, name: 'EditItemTemplate', afterRender: $root.initializeEditPanel }

It seems to be the inclusion of the if statement inside the template definition. If I change the markup to the following, IE8 is happy:

<!-- ko if: $root.itemToEdit.SomeObject() === $data -->
   <!-- ko template: {name: 'EditItemTemplate', afterRender: $root.initializeEditPanel } -->
   <!-- /ko -->
<!-- /ko -->

Why does including an if statement in my template not work in IE8?


回答1:


Older IE versions can be picky about using JavaScript reserved words for property names. If you specify the if like 'if', then you will be fine. Like:

<!-- ko template: {'if': $root.itemToEdit.SomeObject() === $data, name: 'EditItemTemplate', afterRender: $root.initializeEditPanel } -->
<!-- /ko -->

Or something like <label data-bind="attr : { 'for': id }"></label>



来源:https://stackoverflow.com/questions/10140236/using-conditional-knockout-templates-with-ie8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!