2017-04-05 49 views
0

按鈕上的文字我有一個表在多個行HTML低於angularjs改變點擊

jobname  | jobid | Action 
sendemail | 34636 | Button(Name Retry) 
sendereport | 35455 | Button(Name Retry) 

一些數據格式角的js控制我想編寫一個函數,當我將重試按鈕,點擊它會調用一些API調用,我寫了一個,但我需要什麼時,我會點擊重試按鈕,它的按鈕上的文字將變爲重試... 如何唯一標識角JS控制器,每個按鈕

+0

是排在'NG-repeat'?然後你可以指定一個例如重複項目上的'boolean'屬性,並用它來知道要顯示哪些文本。如果你顯示相關代碼,這將有所幫助。 – Arg0n

回答

1

可以使用ng-repeat來顯示數據,點擊按鈕時可以更改該按鈕的名稱。

假設這是你的陣列

$scope.arr = [{"jobname":"sendemail","jobid":123, "Action":"btn1"},{"jobname":"sendReport","jobid":123, "Action":"btn2"},{"jobname":"sendWhatever","jobid":123, "Action":"btn3"}] 

您可以用NG-重複這樣

<tr ng-repeat="item in arr"> 
    <td>{{item.jobname}}</td> 
    <td>{{item.jobid}}</td> 
    <td><button ng-click="clickFunc(item)">{{item.Action}}</button></td> 
</tr> 

在點擊顯示在DOM中的數據,函數傳遞對象作爲參數,並更改按鈕值

$scope.clickFunc = function(item){ 
    item.Action = "retry" 
} 

演示

angular.module("app",[]) 
 
.controller("ctrl",function($scope){ 
 
$scope.arr = [{"jobname":"sendemail","jobid":123},{"jobname":"sendReport","jobid":123},{"jobname":"sendWhatever","jobid":123}] 
 

 
$scope.clickFunc = function(item){ 
 
    item.Action = "retry" 
 
} 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="app" ng-controller="ctrl"> 
 
<table> 
 
<tr> 
 
    <td>jobname</td> 
 
    <td>jobid</td> 
 
    <td>Action</td> 
 
<tr ng-repeat="item in arr"> 
 
    <td>{{item.jobname}}</td> 
 
    <td>{{item.jobid}}</td> 
 
    <td><button ng-init="item.Action = 'btn'" ng-click="clickFunc(item)">{{item.Action}}</button></td> 
 
</tr> 
 
</table> 
 
</div>

+0

我們可以在刪除「Action」後執行此操作:「btn1」「Action」:「btn2」等等..從$ scope.arr開始並執行此操作? –

+0

在這種情況下,我們不能但是這種方法有什麼問題 –

+0

我從mongodb獲取所有數據,我沒有從db –

0

您可以使用如下(服務呼叫期間基本例子):

$scope.buttonLabel = "Retry"; 
 

 
$scope.getServiceDealData = function() { 
 
     $scope.buttonLabel = "Retrying.."; 
 

 
     yourDataService.getYourServiceData().then(function (data) { 
 
      $scope.dealData = data.deals; 
 
      $scope.buttonLabel = "Retry"; 
 
     }).catch(function (data) { 
 
      $scope.errorMessage = "Oops! Something went wrong."; 
 
      $scope.buttonLabel = "Retry"; 
 
     }); 
 
    }

+0

html是哪裏? –

3

您可以通過使用納克級,並在做簡單的方法。 這是您的index.html

<!DOCTYPE html> 
<html lang="en-US" ng-app="myApp"> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
<script src="script.js"></script> 
<style> 
    .red{ 
    color:red; 
    } 
</style> 
<body> 

<div ng-controller="myCtrl"> 
    <button ng-click="abc=1" ng-class="{red:abc>0}">Click me</button> 
</div> 

</body> 
</html> 

,這將是的script.js

angular.module('myApp',[]) 
.controller('myCtrl',function($scope){ 

}) 

沒有必要寫特殊功能的控制器改變顏色。 ng-class提供了內置功能。

我還創建了一個普拉克 https://plnkr.co/edit/Koh1m6?p=preview