2017-03-02 60 views
0

下面的演示應用程序顯示了三個不同的進度條。示波器型號更新

現在用戶需要選擇他/她想要更改值 的哪個進度條,然後點擊在同一頁面上提供的按鈕。

var app = angular.module('myApp',[]); 
 

 
app.component('listComponent', { 
 

 
    template:'<div ng-repeat="progress in $ctrl.obj.bars track by $index">' + 
 
    '<progress value="{{progress}}" max="{{$ctrl.obj.limit}}">{{progress}}</progress><br>'+ 
 
    '</div>'+ 
 
    '<br>' + 
 
    '<div>' + 
 
    'Selected Progressbar : {{$ctrl.selectedProgressbar}}' + 
 
    '<span>' + 
 
    '<select name="selectedProgressbar" ng-model="$ctrl.selectedProgressbar">' + 
 
    '<option ng-repeat="progress in $ctrl.obj.bars track by $index" value="{{$index}}">{{progress}}</option>' + 
 
    '</select>' + 
 
    '</span>' + 
 
    '<span ng-repeat="btn in $ctrl.obj.buttons">' + 
 
    '<button class="btn" ng-click="$ctrl.changeProgress(btn, $ctrl.selectedProgressbar)">{{btn}}</button>' + 
 
    '</span>' + 
 
    '</div>', 
 
    controller: function() { 
 

 
    this.obj = { 
 
     "buttons": [ 
 
     10, 
 
     38, 
 
     -13, 
 
     -18 
 
     ], 
 
     "bars": [ 
 
     62, 
 
     45, 
 
     62 
 
     ], 
 
     "limit": 230 
 
    }; 
 

 
    function changeProgressbar(val){ 
 
     var val = parseInt(val); 
 
     var barValue = this.obj.bars[this.selectedProgressbar]; 
 
     var selectedBar = this.selectedProgressbar; 
 
     var bars = this.obj.bars; 
 

 
     // this.obj.bars[0] = parseInt(this.obj.bars[0]) + parseInt(val); 
 
     // if we remove comment from above code and comment below one then progresbar value changes at same time 
 
     // but with below code its not changing at same time its changing when we click on any button or change progreebar selection 
 
     if(val > 0){ 
 
     var total = parseInt(barValue) + val; 
 

 
     var update = setInterval(function() { 
 
      if (parseInt(barValue) > total) { 
 
      clearInterval(update); \t \t \t \t 
 
      } 
 
      barValue = parseInt(barValue) + 1; 
 
      bars[selectedBar] = barValue; 
 
     }, 15); 
 
     } 
 
    } 
 
    this.changeProgress = changeProgressbar; 
 
    } 
 
});
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<meta name="description" content="This is just demo application by using Angular 1.6"> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width"> 
 
    <title>Progressbar in Angular 1.6</title> 
 
    <style type="text/css" media="screen"> 
 
\t \t progress:after { 
 
\t \t  display: block; 
 
\t \t  content: attr(value); 
 
\t \t  text-align:center; 
 
\t \t } \t \t 
 
    </style> 
 
</head> 
 
<body ng-app="myApp"> 
 
    <list-component></list-component> 
 
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script> 
 
    <script> 
 
    
 
    </script> 
 
</body> 
 
</html>

jsBin在這裏,

現在選擇的任何進度後,然後點擊任何前兩個按鈕則沒有變化上progreebar

找到,但你點擊儘快再次選擇一些其他的進度條,然後值正在改變。

+0

沒有更好的做到這一點爲指令,而不是作爲一個應用程序 –

回答

0

通過你的代碼後,我發現了一些問題。

您應該更改changeProgressbar函數並刪除間隔函數。

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
<meta name="description" content="[add your bin description]"> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width"> 
 
    <title>Progressbar in Angular 1.6</title> 
 
    <style type="text/css" media="screen"> 
 
\t \t progress:after { 
 
\t \t  display: block; 
 
\t \t  content: attr(value); 
 
\t \t  text-align:center; 
 
\t \t } \t \t 
 
    </style> 
 
</head> 
 
<body ng-app="myApp"> 
 
    <list-component></list-component> 
 
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script> 
 
    <script> 
 
    var app = angular.module('myApp',[]); 
 
    app.component('listComponent', { 
 
     // isolated scope binding 
 
     template:'{{$ctrl.obj.bars}}<div ng-repeat="progress in $ctrl.obj.bars track by $index">' + 
 
        '<progress value="{{progress}}" max="{{$ctrl.obj.limit}}">{{progress}}</progress><br>'+ 
 
       '</div>'+ 
 
       '<br>' + 
 
       '<div>' + 
 
        'Selected Progressbar : {{$ctrl.selectedProgressbar}}' + 
 
        '<span>' + 
 
         '<select name="selectedProgressbar" ng-model="$ctrl.selectedProgressbar">' + 
 
          '<option ng-repeat="progress in $ctrl.obj.bars track by $index" value="{{$index}}">{{progress}}</option>' + 
 
         '</select>' + 
 
        '</span>' + 
 
        '<span ng-repeat="btn in $ctrl.obj.buttons">' + 
 
         '<button class="btn" ng-click="$ctrl.changeProgress(btn, $ctrl.selectedProgressbar)">{{btn}}</button>' + 
 
        '</span>' + 
 
        '</div>', 
 
     controller: function() { 
 
    
 
      this.obj = { 
 
       "buttons": [ 
 
        10, 
 
        38, 
 
        -13, 
 
        -18 
 
       ], 
 
       "bars": [ 
 
        62, 
 
        45, 
 
        62 
 
       ], 
 
       "limit": 230 
 
      }; 
 
    
 
      function changeProgressbar(val){ 
 
       var val = parseInt(val); 
 
       var barValue = this.obj.bars[this.selectedProgressbar]; 
 
       var selectedBar = this.selectedProgressbar; 
 
       var bars = this.obj.bars; 
 
       
 
       // this.obj.bars[0] = parseInt(this.obj.bars[0]) + parseInt(val); 
 
       // if we remove comment from above code and comment below one then progresbar value changes at same time 
 
       // but with below code its not changing at same time its changing when we click on any button or change progreebar selection 
 
       if(val > 0){ 
 
        var total = parseInt(barValue) + val; 
 
         
 
         if (parseInt(barValue) > total) { 
 
          clearInterval(update); \t \t \t \t 
 
         } 
 
        else 
 
        { 
 
         barValue = total; 
 
         bars[selectedBar] = barValue; 
 
        } 
 
        
 
       } 
 
      } 
 
      this.changeProgress = changeProgressbar; 
 
     } 
 
    }); 
 
    </script> 
 
</body> 
 
</html>

請執行上面的代碼中

Here is a working DEMO

+0

@Nitish,請檢查我的答案。 – Sravan