将JavaScript split()函数传递给ng-model

时间:2016-08-25 18:11:09

标签: javascript angularjs angularjs-directive

我有一个绑定表达式,它使用JavaScript split()方法获取字符串中的值。例如{{ failureDetail.name.split('#')[0]}}。这将返回merchantId(#sorted string的第0个索引是merchantId)。这很好用。当我尝试将此值传递给 ng-model 指令时,它不起作用。这里的目的是基于merchantId进行过滤。问题似乎是, split()函数在ng-model指令中不起作用。 建议的方法是什么。代码如下。

 <tr ng-repeat-start="failureDetail in failureDetails | filterByTags:tags | filter:merchantStatus">
                          <td>
                             <button class="btn btn-warning" ng-if="failureDetail.expanded" ng-click="failureDetail.expanded = false"><i class="material-icons">remove</i></button>
                             <button class="btn btn-success" ng-if="!failureDetail.expanded" ng-click="failureDetail.expanded = true"><i class="material-icons">add</i></button>
                          </td>
                          <td title="ID" filter="{ failureDetail.name.split('#')[0]: 'text'}" sortable="'failureDetail.name.split('#')[0]'" >{{failureDetail.name.split('#')[0]}}</td>
                          <td title="Name" filter="{failureDetail.name.split('#')[1] : 'text'}" sortable="'failureDetail.name.split('#')[1]'">{{failureDetail.name.split('#')[1]}}</td>
                          <td title="AB" filter="{failureDetail.name.split('#')[4] : 'text'}" sortable="'failureDetail.name.split('#')[4]'">{{failureDetail.name.split('#')[4]}}</td>
                          <!--<td>{{failureDetail.name.split('#')[2]}}</td
                            <td title="'ID'" filter="{ ID: 'text'}" sortable="'ID'">
                          >-->
                          <td title="CD" filter="{failureDetail.name.split('#')[3] : 'text'}" sortable="'failureDetail.name.split('#')[3]'">{{failureDetail.name.split('#')[3]}}</td>

                       </tr>

1 个答案:

答案 0 :(得分:0)

您需要将merchandId保存在控制器变量中:

$scope.merchantId = $scope.failureDetail.name.split('#')[0]

然后,您将能够做您想做的事:ng-model="merchantId"

希望有所帮助