2017-03-02 62 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