Knockout 組件嵌套模板書寫注意問題,knockout嵌套

來源:互聯網
上載者:User

Knockout 組件嵌套模板書寫注意問題,knockout嵌套

在進行Knockout組件嵌套模板編寫時,會有多個引號的使用,特別是單引號和雙引號同時使用時,這裡有一個例子【1】:

    templateEngine.addTemplate("ko_simpleGrid_grid", "\            <table class=\"ko-grid\" cellspacing=\"0\" data-bind=\"\">\                <thead>\                    <tr data-bind=\"foreach: columns\">\                       <th><div><span class=\"adjustCol_left\"> </span><span data-bind=\"text: headerText\"></span> \                       <span class=\"adjustCol_right\" data-bind=\"event:{mousedown:$parent.headTdMousedown}\"> </span></div></th>\                    </tr>\                </thead>\                <tbody data-bind=\"foreach: itemsOnCurrentPage\">\                   <tr data-bind=\"foreach: $parent.columns\">\                       <td data-bind=\"attr:{ prop1:typeof rowText == 'function' ? rowText($parent) : $parent[rowText] }, click:$root.cellEdit \"> \                       <div data-bind='component: {name:\"ColumnEditor\", params:{editortype:editortype, value:typeof rowText == \"function\" ? rowText($parent) : $parent[rowText], editing:false}}'></div>\                       </td>\                    </tr>\                </tbody>\            </table>");

在這裡,注意這一行:

(1)正確的寫法

<div data-bind='component: {name:\"ColumnEditor\", params:{editortype:editortype, value:typeof rowText == \"function\" ? rowText($parent) : $parent[rowText], editing:false}}'></div>\
value的賦值邏輯為:判斷表格模型的父模型rowText資料類型是否為function,若為function則執行該函數擷取值;否則使用父模型中的值。在這裡整個一行字串包含在大的模板字串中,data-bind使用了單引號來書寫綁定內容,這時,若再在綁定裡使用單引號,則需要轉義,如上行(1)。

假如寫成這樣,則是錯誤的:

(2)錯誤的寫法

<div data-bind='component: {name:\"ColumnEditor\", params:{editortype:editortype, value:typeof rowText == 'function' ? rowText($parent) : $parent[rowText], editing:false}}'></div>\
雖然這是一個本應該注意的問題,但在使用情境較複雜的情形,很容易忘記轉義,而錯誤的寫法卻導致錯誤非常難查。

作為經驗,進行記錄,以供碰到類似問題時作為參考。


參考:

【1】Page Grid:http://knockoutjs.com/examples/grid.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.