Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: apply filter for ngStyle

Tags:

angularjs

Good day for everyone!

I have a problem with understanding AngularJS. Can I use my custom filter within ngStyle directive? Why it can't change opacity of span tag at the same time when I change value in input (but it change value in markup)? How I can realize this behaviour without direct using controller scope?

My raw code: HTML:

<div ng-app="app">
    <div ng-controller="AppCtrl">
        <input type="number" ng-model="slider" max="10" min="1">
        <span ng-style="{'opacity': '{{slider | filter}}'}">TEXT</span>
    </div>
</div>

JS:

(function () {
    angular
        .module('app', [])
        .controller('AppCtrl', ['$scope', function ($scope) {
            $scope.slider = 6;
        }])
        .filter('filter', function () {
            return function (input) {
                return 0.1 * input;
            };
        });
})();

My code at JSFiddle: http://jsfiddle.net/zkdkLac3/

like image 638
magmel Avatar asked Oct 23 '14 17:10

magmel


People also ask

How do I use NgStyle directive?

The NgStyle directive lets you set a given DOM elements style properties. This sets the background color of the div to green. The above code uses the ternary operator to set the background color to green if the persons country is the UK else red.

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.

What is the difference between style and NgStyle?

ngStyle is an Angular directive that gives you the flexibility to do this, where as style is a regular HTML property to which you can only bind values one by one. That's the difference between the two of them.

Which AngularJS filter selects a subset of items from an array?

AngularJS Filters filter Select a subset of items from an array. json Format an object to a JSON string. limitTo Limits an array/string, into a specified number of elements/characters. lowercase Format a string to lower case.


1 Answers

Answering the general question, yes, generally you can use an user created filter in generic angular expressions. You might be having issues with ng-attr due to a parsing error (probably a bug in the angular parser). You can still use filters in ng-attr with

<span ng-style="{ 'opacity': (slider | opacity) }">TEXT</span>

ng-attr though is most beneficial for binding to style objects directly

<span ng-style="sliderStyle">TEXT</span>

you can also style directly by using

<span style="opacity: {{slider|opacity}}">TEXT</span>

with the below filter:

app.filter('opacity', function () {
    return function (input) {
       return 0.1 * input;
    };
});

Working jsfiddle

Whichever solution is better mainly depends on where you plan to re-use things. Filters are available across all scopes, but this one in particular might only make sense for a given controller. Don't forget that reuse can be accomplished with directives (which can have a controller) as well.

like image 162
00500005 Avatar answered Oct 04 '22 12:10

00500005