1

I wanted to have some dynamic styling to accommodate incoming user badges. It works in Rails but wanted to do something in Angular. Thoughts?

<div ng-repeat="event in events">
<style ng-repeat="product in event.products">
  .product-id-{{ product.id }} .fa-gift { display: inline-block !important; }
  .product-id-{{ product.id }} .fa-star { display: inline-block !important; }
  .product-id-{{ product.id }} .{{ product.ticket_type }} { display: inline-block !important; }
</style>
<style ng-repeat="product in event.products" ng-if="product.ticket_type == 'golden'">
  .product-id-{{ product.id }} .{{ product.ticket_type }} { display: none !important; }
  .product-id-{{ product.id }} .icon-crown.golden { display: inline-block !important; }
</style>
<style ng-repeat="product in event.products" ng-if="product.ticket_type == 'vip'">
  .product-id-{{ product.id }} .{{ product.ticket_type }} { display: none !important; }
  .product-id-{{ product.id }} .icon-crown.golden { display: none !important; }
  .product-id-{{ product.id }} .fa-diamond.vip { display: inline-block !important; }
</style>
</div>

This doesn't currently resolve values.

1

2 Answers 2

4

I'm not sure that using angular to set variables in inline styles is necessarily the best approach, but what you described is definitely possible:

html

<body ng-controller="MainCtrl">
  <p>color is {{color}}!</p>
  <style>
    p {
      color: {{color}};
    }
  </style>
</body>

js

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

app.controller('MainCtrl', function($scope) {
  $scope.color = 'blue';
});

http://plnkr.co/edit/cTSqgX?p=preview

enter image description here

I suspect there is a scoping issue with your provided example code

Sign up to request clarification or add additional context in comments.

3 Comments

i added the ng-repeats in case its insightful. the scoping works outside the <style> tag for me <div ng-repeat="event in events"> <div ng-repeat="product in event.products"> {{ product.title }} <!-- {{ product.title }} --> <style type="text/css"> <!-- .product-id-{{ product.id }} .fa-gift { display: inline-block !important; } .product-id-{{ product.id }} .fa-star { display: inline-block !important; } .product-id-{{ product.id }} .{{ product.ticket_type }} { display: inline-block !important; } --> </style>
Could be misaligned variables with your data. Here, I expanded my answer plunk to include your provided code: plnkr.co/edit/AtBUHd?p=preview
awesome! thats exactly what i want to do. appreciate it!
1

like this? http://jsfiddle.net/leojavier/4wwuoLxs/

<div class="field" ng-app="App">
    <table ng-controller="Controller">
        <tr ng-repeat="item in table">
            <td ng-class="item.style" ng-class="item.badge"><i ng-class="item.badge"></i> {{item.name}}</td>
        </tr>
    </table>
</div>

angular JS

var App = angular.module('App', []);
App.controller('Controller', function($scope, $timeout){
    $scope.table = [
        {name : 'Dan', deleted:false},
        {name : 'Orlando'},
        {name : 'Dany'}
   ];

    $scope.delete = function(item, index){
        item.deleted = true;
        item.style = 'deleted';

        function destroy() {
        $scope.table.splice(index, 1)
        }

        $timeout(function(){destroy();}, 3000);
    }
})

css

body{
    font-family:arial;
}
a{
    text-decoration:none;
    color:red;
}

table{
width:300px;
}

td{
    border:thin solid #CCC;
    padding:10px;

}

tr{
 position:relative   
     width:300px;
}

.deleted:after{
    content:'DELETED';
    position:absolute;
    top:0;
    left:0;
    background:red;
    width:300px;
    color:#FFF;
    text-align:center;
    line-height:40px;
}

You can see it working here: http://jsfiddle.net/leojavier/4wwuoLxs/

1 Comment

In the example I'm loading fortawesome.github.io/Font-Awesome/icons to show the different icons...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.