Model view controller 无法将切片应用于viewmodel敲除

Model view controller 无法将切片应用于viewmodel敲除,model-view-controller,knockout.js,Model View Controller,Knockout.js,由于以下错误消息,我无法将slice或substr应用于GId: Uncaught TypeError: Unable to process binding "foreach: function (){return waititem }" Message: Unable to process binding "style: function (){return { color:GId.slice(0,1) =='TR'?'black':'red'} }" Message: undefined i

由于以下错误消息,我无法将slice或substr应用于
GId

Uncaught TypeError: Unable to process binding "foreach: function (){return waititem }"
Message: Unable to process binding "style: function (){return { color:GId.slice(0,1) =='TR'?'black':'red'} }"
Message: undefined is not a function 
Html

<table>
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: waititem">
        <tr data-bind="style: { color: GId.slice(0,1) == 'TR'? 'black' : 'red' }">
            <td data-bind="text: PId"></td>
            <td data-bind="text: PName"></td>
        </tr>
    </tbody>
</table>

身份证件
名称

如何应用slice来检查是否存在特定的子字符串并分别设置行的样式?

您在
样式
绑定中错误地使用了slice方法。您需要使用slice的语法作为

slice(start, end)
其中,“开始”的初始位置默认为0,“结束”将是结束字符。在您的场景中,切片将返回一个长度为1的字符串,正如您使用的
切片(0,1)
一样,但您正在与长度为2的
'TR'
进行比较。相反,您需要使用
slice(0,2)='TR'

 <tr data-bind="style: { color: GId.slice(0,2) == 'TR'? 'black' : 'red' }">
      <td data-bind="text: PId"></td>
      <td data-bind="text: PName"></td>
 </tr>


显示您的view-modelviewmodel.waititem是observablearray并包含此模型公共类的集合访问{public string PName{get;set;}public string PId{get;set;}public string GId{get;set;}如果将样式:{..}替换为样式:{$parent.getColor}getColor是一个类似于此的函数:self.getColor=function(data){return data.GId.slice(0,1)='TR'?'black':'red';}从注释中的语法来看,您似乎正在使用,如果是这样的话,应该相应地标记问题。拥有实际数据会有所帮助,例如,将
的输出置于
上方时。这是一个问题,但不是OP得到错误的原因。