Yasir
Yasir

Reputation: 909

Angular within Style Tags

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.

Upvotes: 1

Views: 975

Answers (2)

wesww
wesww

Reputation: 2873

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

Upvotes: 4

Leo Javier
Leo Javier

Reputation: 1403

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/

Upvotes: 1

Related Questions