【问题标题】:Knockout.js binding to json object memebr prefixed with @Knockout.js 绑定到以 @ 为前缀的 json 对象 memebr
【发布时间】:2013-01-09 09:40:21
【问题描述】:

我已经使用 JSON.Net 将一些 XML 转换为 Json,然后使用 Knockout.js 绑定到一个视图。

我的问题是我的 XML 属性以 json 表示,前缀为 @,在 Knockout.js 中被视为非法字符。

我的视图模型有以下内容:

self.titles = ko.computed(function () {
     var str = self.searchForText().toLowerCase();

     return jsonString.AutoPolicy.Policy.filter(function (el) {                           
             return el['@id'].toLowerCase().indexOf(str) == 0;
     });
}, self);

还有我的html:

    <div id="searchResultsDiv" class="sectionDiv">             
         <div data-bind="foreach: titles">                         
            <div data-bind="text: @id, click: $parent.isSelected, event : { dblclick: $parent.openFileDblClick }"></div>                                                             
         </div>   
    </div>

如何绑定到属性?是否有转义键或从视图模型返回的替代方法?

编辑

我已经修改了我的视图模型,添加了一个可以绑定到的元素:

 // bind a list to json data **NEEDS TO VE ALL TITLES**
 self.titles = ko.computed(function () {
      var str = self.searchForText().toLowerCase();                      
         jsonString.AutoPolicy.Policy['@id']

         return jsonString.AutoPolicy.Policy.filter(function (el) {
            el.id = el['@id'];               
            return el['@id'].toLowerCase().indexOf(str) == 0;
         });
      }, self);

这给出了预期的结果,但有更好的方法吗?

谢谢

【问题讨论】:

    标签: json knockout.js xml-serialization


    【解决方案1】:

    试试这样的:

    <div data-bind="text: $data['@id']"></div>
    

    【讨论】:

    • 优秀。这就是我要找的;谢谢。
    猜你喜欢
    • 1970-01-01
    • 2015-04-17
    • 1970-01-01
    • 2012-04-08
    • 2016-05-15
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多