angularjs - 如何更改 angularjs 中每行的按钮文本

标签 angularjs

我正在从 angularjs 中的其余服务获取数据,并且数据使用 ng-repeat 以表格的形式显示。 我正在根据变量的值创建一个激活/停用按钮,如果该值为 true,则显示激活按钮,否则显示停用按钮。 这是我的 Controller

   $scope.btnText='Deactivate';
      $scope.active={};
     FetchDomainService.get(function(
       response) {
        $scope.domains = response;
        $scope.active=$scope.domains[0].isActive;
          if(  $scope.active==='true'){
  $scope.btnText==='Activate';
     }
    });

这是我的 html 代码

   <tr ng-repeat="domain in domains">

                <td>{{domain.clientId}}</td>
                <td>{{domain.jndisys}}</td>
                <td>{{domain.buildVersion}}</td>
                <td>{{domain.domainUuid}}</td>
                <td>{{domain.isActive}}</td>
                <td>

                    <button class="btn"
                        ng-click="">
         <span class="glyphicon glyphicon-pencil"></span>{{btnText}}
                    </button>
                </td>

            </tr>

现在的问题是...我如何更改每一行中的 btn 文本..我可以在所有行中更改它,但不能在单行中更改它。我需要检查每一行,然后更改按钮文本。 isActive 是一个 bool 值,如果为 true,则按钮文本为“激活”;如果 isActive 值为 false,则按钮文本为“停用”。

最佳答案

您可以在收到数据时向数据中的每个项目添加一些字段,例如 isActive。然后根据该字段的值在 ng-repeat 显示按钮。 您可以使用 native JavaScript 函数作为 mapforEach 来添加此值。

例如:

  FetchDomainService.get(
    function (resp) {
      $scope.items = resp.map(function (item) {
        item.isActive = true;
        return item;
      });
    }
  );

要显示/隐藏文本 - 使用此值 isActiveAngularJS 指令 ng-showng-hideng-if

<span ng-hide="item.isActive">Activate</span>
<span ng-show="item.isActive">Deactivate</span>

演示:http://plnkr.co/edit/cYm0R8r4iFygk9A8iVPj?p=preview

关于angularjs - 如何更改 angularjs 中每行的按钮文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33234447/

相关文章:

javascript - 我不相信 Angular UIrouter 正在路由到我的 View

javascript - AngularJS/UI Bootstrap - 删除时淡出警报

javascript - 将一个文本框上的属性传递给另一个 div 按钮的 Angular 指令

angularjs - 测试具有许多依赖项的 Angular Controller

javascript - 在 angularjs 页面中显示范围内的元素

angularjs - 具有 ES6 功能的面向 future 的 Angular 应用

html - 为什么 HTML Div 标签之间的边距随机

html - 链接以 Angular Material 填充侧边栏的整个宽度

javascript - 检测对自定义指令的关注

arrays - 访问数组内的数组 (ng-repeat)