Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to style element width % using ng-style

I want to style my progress bar using with percentages but following ng documentation I can't seem to grasp it. It should be 20% with, but it's 100% (default).

Fiddle: http://jsfiddle.net/u6xp8csh/

Here is what I have tried

HTML

<div data-ng-app>
    <div data-ng-controller="ProgressBarController">
        <div class="progress-bar-container">
            <div class="progress-bar" ng-style="{'width' : '{{ progress }}'% }">{{ progress }}</div>
        </div>
    </div>
</div>

JS

function ProgressBarController($scope) {
    $scope.progress = 20;
}

CSS

.progress-bar-container {
    width: 300px;
    height: 100px;
    box-sizing: border-box;
    border: 1px solid black;
}
.progress-bar {
    height: 100px;
    background-color: green;
}
like image 544
Stan Avatar asked Dec 08 '14 13:12

Stan


People also ask

Can we use NgClass and NgStyle together?

You can combine conditional styling with Angular's property and event binding by using NgStyle and NgClass directives using the Angular template syntax.

What is the difference between NgClass and NgStyle?

The NgClass and NgStyle directives are used to apply style to the HTML element conditionally. The NgClass allows you to apply whole class based on condition whereas NgStyle gives you more flexibility to set individual properties.

How do you set up NgStyle?

NgStylelinkAn attribute directive that updates styles for the containing HTML element. Sets one or more style properties, specified as colon-separated key-value pairs. The key is a style name, with an optional . <unit> suffix (such as 'top.

How do you use important in NgStyle?

ng-style does not support ! important . So alternate is to use ng-class instead of ng-style , it will solve the problem. If you want to use ng-style specifically then try to write within html itself- please don't try to write within any function.


1 Answers

progress field accessible without '{{':

 ng-style="{'width' : progress + '%' }"

http://jsfiddle.net/gc343w7x/

like image 150
Rasalom Avatar answered Sep 19 '22 04:09

Rasalom