angularjs获取标签内容

 <tr ng-repeat="goods in $data">
    <td contenteditable='true'>
            {{goods.name}}
    </td>
 </tr>
 
 对td内容进行修改
 
 现在我想获取点击td 弹出当前td的内容。
 
 怎么实现。
阅读 6.1k
3 个回答
<tr ng-repeat="goods in $data">
    <td ng-click="show($event)" contenteditable="true" >
         {{goods.name}}
    </td>
 </tr>
 
function show(e){
    alert(e.target.innerText)
}

ng-model是双向数据绑定可以随时获取值,但ng-model只作用于input标签和一些文本标签上。所以根据你的需求,只能通过dom去获取文本上的内容,但不是实时的,这一些修改的值下次点击才会得到。
<tr ng-repeat="goods in $data track by $index">
    <td ng-click=consoleSomeThing($index)>
            {{goods.name}}
    </td>
 </tr>

//js部分
$scope.consoleSomeThing=function(index){
   //具体看你的$data结构了,复杂点的就去遍历吧,反正重点是拿到对应的索引index
    console.log($data[index].goods.name)
}

使用
name = angular.element($event.target).text()

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进