It is very simple, with a directive you $watch
the scope
for your variable (defined in the attribute of your element), and when it changes you change the value in your element
's attribute.
plnkr.co code example
HTML:
<div class="progress">
<div class="progress-bar" role="progressbar" progress-bar-watch="progress" progress-bar aria-valuenow="" aria-valuemin="0" aria-valuemax="100" style=""></div>
</div>
JS
app.controller('MainCtrl', function($scope) {
$scope.progress = 0;
// Just to show that changing the value dynamically updates the DOM..
var reversing = false;
window.setInterval(function() {
$scope.$apply(function() {
if (reversing) {
$scope.progress -= 1;
} else {
$scope.progress += 1;
}
if ($scope.progress >= 100) {
reversing = true;
} else if ($scope.progress <= 0) {
reversing = false;
}
});
}, 100)
})
.directive('progressBar', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var watchFor = attrs.progressBarWatch;
// update now
var val = scope[watchFor];
element.attr('aria-valuenow', val)
.css('width', val+"%");
// watch for the value
scope.$watch(watchFor, function(val) {
element.attr('aria-valuenow', val)
.css('width', val+"%");
})
}
}
})