使用Knockout.JS链接更正两个id的语法

时间:2013-08-27 17:12:40

标签: syntax knockout.js hyperlink

好的,所以我可以使用一个id创建一个链接:

<td><a data-bind="text: productTypeId, attr: {href: '/*******WebAdmin2/ProductManager/ProductTypeDescriptionEditor.aspx?ProductTypeID=' + productTypeId}" target="_new"></a></td>

但是,在链接中引用两个id的正确synatx是什么?

<td><a data-bind="text: wsNotes, attr: {href: '/****webadmin2/Common/PopupWindows/ManufacturerBlurbEditor.aspx?manufacturerid= + manufacturerBlurbID, &stylecode=styleCodeId'}" target="_new">Edit Blurb</a></td> 

抱歉!我对KO还很新。谢谢你的帮助!

1 个答案:

答案 0 :(得分:2)

你可以这样做:

<td><a data-bind="text: wsNotes, attr: { href: '/****webadmin2/Common/PopupWindows/ManufacturerBlurbEditor.aspx?manufacturerid=' + manufacturerBlurbID + '&stylecode=' + styleCodeId }" target="_new">Edit Blurb</a></td> 

更好的方法是将url创建逻辑从视图移动到viewModel,如:

var MyViewModel = function (data) {
    this.styleCodeId = ko.observable(data.styleCodeId);
    this.manufacturerBlurbID = ko.observable(data.manufacturerBlurbID);

    this.manufacturerUrl = ko.computed(function () {
        return '/****webadmin2/Common/PopupWindows/ManufacturerBlurbEditor.aspx?manufacturerid=' + this.manufacturerBlurbID() + '&stylecode=' + this.styleCodeId();
    }, this);
};

在您看来,您可以像这样引用它:

<td><a data-bind="text: wsNotes, attr: { href: manufacturerUrl }" target="_new">Edit Blurb</a></td>