Angularjs:如何捕获 <td> 标签中的文本并在我的控制器中使用它

Angularjs: How to capture the text in <td> tag and use it inside my controller

本文关键字:我的 控制器 文本 标签 何捕获 td Angularjs      更新时间:2023-09-26

我需要捕获按下编辑按钮的行的 id 和标题值。

以下是我的ng-repeat的结构:

 <tr ng-repeat="post in posts">
      <td>
       <div class="ui singlenum checkbox">
        <input type="checkbox" id="unique-1">
        <label for="unique-1">{{post.id}}</label>
       </div>
      </td>
      <td>{{post.title}}</td>
      <td>{{post.posted_on}}</td>
      <td>{{post.is_moderated}}</td>
      <td>
            <div id="editbtn" ng-click="showCustom()" class="circular ui icon button">
              <i class="edit icon"></i>
            </div>
      </td>
    </tr>

这是我的控制器代码的一部分,我需要在其中访问特定的{{post.id}}{{post.title}}值。

app.controller('SampleController', ['$scope', 'ModalService', function($scope, ModalService) {
$scope.showCustom = function() {
 ModalService.showModal({
      templateUrl: "complex/complex.html",
      controller: "ComplexController",
      inputs: {
        id: // This is where I need to specify the id for which the edit button was pressed.
        title: // This is where I need to specify the title for which the edit button was pressed.
      }
    }).then(function(modal) {
      modal.element.modal();
      modal.close.then(function(result) {
        $scope.complexResult  = "Name: " + result.name + ", age: " + result.age;
      });
    });
  };

我需要访问label中的post.id和包装在<td>元素中的post.titleng-model不适用于这些类型的元素,所以我不能使用它。

你可以

showCustom(post)一样post参数传递给你的函数。

<div id="editbtn" ng-click="showCustom(post)" class="circular ui icon button">
  <i class="edit icon"></i>
</div>

您可以在控制器中使用它。

$scope.showCustom = function(post) {
    var postId = post.id; //Get id
    var title = post.title; //Get Title
    //Rest of your code
}