1

我有以下html片段:

<tbody data-bind="foreach: ptData">
            <tr id="Tr1">
                <td><a data-bind="attr: {href: '/ProjectTrack/IssueGrid.aspx?IssueID='+ptId}">PT Launch</a></td>
                <td nowrap="nowrap" data-bind="text: ptId">
                </td>
                <td nowrap="nowrap" data-bind="text: ptProject">
                </td>
                <td nowrap="nowrap" data-bind="text: ptTitle">
                </td>
                <td nowrap="nowrap" data-bind="text: ptPriority">
                </td>
                <td nowrap="nowrap" data-bind="text: ptType">
                </td>
                <td nowrap="nowrap" data-bind="text: ptStatus">
                </td>
                <td nowrap="nowrap" data-bind="text: ptUpdated">
                </td>
                <td nowrap="nowrap" data-bind="text: ptAssignedTo">
                </td>
                <td style="display: none" data-bind="text: ptUrl">
                </td>
            </tr>
        </tbody>

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

var viewModel = {
        ptData: ko.observableArray([])
    };



function DataResult(ptId, ptProject, ptTitle, ptPriority, ptType, ptStatus, ptUpdated, ptAssignedTo, ptDesc, ptUrl) {
        this.ptId = ko.observable(ptId);
        this.ptProject = ko.observable(ptProject);
        this.ptTitle = ko.observable(ptTitle);
        this.ptPriority = ko.observable(ptPriority);
        this.ptType = ko.observable(ptType);
        this.ptStatus = ko.observable(ptStatus);
        this.ptUpdated = ko.observable(ptUpdated);
        this.ptAssignedTo = ko.observable(ptAssignedTo);
        this.ptDesc = ko.observable(ptDesc);
        this.ptUrl = ko.observable(ptUrl);
    }

我有一个 jQuery ajax 调用来返回填充在 DataResult 对象中的数据,并且每个 DataResult 对象都被推送到 viewModel.ptData。以下 knockout.js 命令似乎在 HTML 片段中不起作用:

"attr: {href: '/ProjectTrack/IssueGrid.aspx?IssueID='+ptId".  

我到底做错了什么?

4

1 回答 1

0

如果您想绑定到+具有可观察属性 ( ptId) 的表达式(在这里您正在构建链接),您必须像函数一样调用它们。所以你错过了()

<a data-bind="attr: {href: '/ProjectTrack/IssueGrid.aspx?IssueID=' + ptId() }">

但是,如果您要创建一个计算的 observable 来生成链接:

function DataResult(ptId, ....) {        
        this.ptId = ko.observable(ptId);
        this.launchLink = ko.computed(function(){
            return '/ProjectTrack/IssueGrid.aspx?IssueID=' + this.ptId()
        }, this);
}

然后你不需要()在你的绑定中,因为现在你直接绑定到一个 observable:

<a data-bind="attr: { href: launchLink }">
于 2013-02-26T05:52:04.267 回答