如何在HTML代码中获取动态变量的值

时间:2015-11-17 10:53:09

标签: javascript html angularjs

我目前正在使用AngularJS,这是我的HTML:

<tbody>
    <tr dir-paginate="tdata in tableData | orderBy:predicate:reverse | filter:searchFilter | itemsPerPage:10 track by $index ">
        <td class="table-data-edit-all">
            <input type="checkbox" ng-model="tdata.selectedCell">
        </td>
        <td class="align-left">
            {{ tdata.companyName }}
        </td>
        <td class="align-left">
            {{ tdata.department }}
        </td>
        <td>
            <a ng-click="editCompany({{ tdata.id }})"><i class="fa fa-edit"></i></a>
            <a ng-click="removeCompany({{ tdata.id }})"><i class="fa fa-remove"></i></a>
        </td>
    </tr>
</tbody>

这是Angular:

$scope.editCompany = function(index) {
    console.log(index);
    // my stuffs
}

加载页面时:

  • 第一次,tdata.id在HTML视图中显示数字标识1 editCompany(index)也会打印到控制台号码1
  • 第二次,在我使用Angular orderBy sort后, tdata.id显示与之对应的号码10 tdata.companyName。但是,此时editCompany(index)仍然存在 打印到控制台号码1,而不是10

我该如何解决?

2 个答案:

答案 0 :(得分:1)

使用ng-click="editCompany(tdata.id)代替ng-click="editCompany({{tdata.id}})

<tbody>
    <tr dir-paginate="tdata in tableData | orderBy:predicate:reverse | filter:searchFilter | itemsPerPage:10 track by $index ">
        <td class="table-data-edit-all">
            <input type="checkbox" ng-model="tdata.selectedCell">
        </td>
        <td class="align-left">
            {{ tdata.companyName }}
        </td>
        <td class="align-left">
            {{ tdata.department }}
        </td>
        <td>
            <a ng-click="editCompany(tdata.id)"><i class="fa fa-edit"></i></a>
            <a ng-click="removeCompany(tdata.id)"><i class="fa fa-remove"></i></a>
        </td>
    </tr>
</tbody>

答案 1 :(得分:0)

哟必须从代码中删除{{}}

在HTML中

<tbody>
<tr dir-paginate="tdata in tableData | orderBy:predicate:reverse | filter:searchFilter | itemsPerPage:10 track by $index ">
    <td class="table-data-edit-all">
        <input type="checkbox" ng-model="tdata.selectedCell">
    </td>
    <td class="align-left">
        {{ tdata.companyName }}
    </td>
    <td class="align-left">
        {{ tdata.department }}
    </td>
    <td>
        <a ng-click="editCompany(tdata.id)"><i class="fa fa-edit"></i></a>
        <a ng-click="removeCompany(tdata.id)"><i class="fa fa-remove"></i></a>
    </td>
</tr>

我希望这能帮到你