如何更改angularjs中每行的按钮文本
我用angularjs从rest服务中获取数据,数据以表格的形式显示,使用ng repeat。 我正在根据变量的值创建一个激活/停用按钮,如果该值为真,则显示激活按钮或停用按钮。 这是我的控制器如何更改angularjs中每行的按钮文本,angularjs,Angularjs,我用angularjs从rest服务中获取数据,数据以表格的形式显示,使用ng repeat。 我正在根据变量的值创建一个激活/停用按钮,如果该值为真,则显示激活按钮或停用按钮。 这是我的控制器 $scope.btnText='Deactivate'; $scope.active={}; FetchDomainService.get(function( response) { $scope.domains = response;
$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>
{{domain.clientId}
{{domain.jndisys}}
{{domain.buildVersion}
{{domain.domainUuid}
{{domain.isActive}}
{{btnText}}
现在的问题是…如何更改每一行中的btn文本..我可以在所有行中更改它,但不能在一行中更改。我需要检查每一行,然后更改按钮文本。
isActive是一个布尔值,如果为true,则按钮文本为“激活”,如果isActive的值为false,则按钮文本为停用。您可以在收到该值时向数据中的每个项目添加一些字段,例如
isActive
。然后在ng中,根据项目的此字段值重复显示按钮。
您可以使用本机JavaScript
函数作为map
或forEach
来添加此值
例如:
FetchDomainService.get(
function (resp) {
$scope.items = resp.map(function (item) {
item.isActive = true;
return item;
});
}
);
要显示/隐藏文本,请使用此值isActive
和AngularJS
指令ng show
,ng hide
或ng if
<span ng-hide="item.isActive">Activate</span>
<span ng-show="item.isActive">Deactivate</span>
激活
使停止工作
演示:您需要尝试以下内容:
<button class="btn" ng-disabled = "domain.isActive">
<span class="glyphicon glyphicon-pencil">{{domain.isActive ? 'btnName1' :'btnName2'}}</span>
</button>
{{domain.isActive?'btnName1':'btnName2'}
你可以在这里得到答案,答案很好地解释了