ui cell模板需要动态的ng风格

时间:2016-10-21 06:47:39

标签: angularjs twitter-bootstrap angular-ui angular-ui-grid

在angularjs ui网格中,我有如下所示的单元格模板:

cellTemplate :  "<div class="progress"> <div class="progress-bar progress-bar-info" ></div> </div>"

现在我需要动态更改cellTemplate的颜色类,即progress-bar-info。基于列值(完整,不完整,正在进行或失败)我需要在cellTemplate中有progress-bar-success, progress-bar-info, progress-bar-warning, progress-bar-danger。我们怎样才能实现它?

我尝试了以下模板,但它给出了错误:

cellTemplate: " <div class=\"progress margin-top-10\"> <div class=\"progress-bar\"   ng-class=\"{progress-bar-success:{{row.entity.status}}==='complete', progress-bar-warning:{{row.entity.status}}==='incomplete', progress-bar-danger:{{row.entity.status}}==='fail'}\"  ng-style=\"{width:'{{row.entity.val}}%'}\"></div> </div>",

错误如下:

angular.js:13920 Error: [$parse:syntax] Syntax Error: Token '-' is unexpected, expecting [}] at column 10 of the expression [{progress-bar-success:complete==='complete', progress-bar-warning:complete==='incomplete', progress-bar-danger:complete==='fail'}] starting at [-bar-success:complete==='complete', progress-bar-warning:complete==='incomplete', progress-bar-danger:complete==='fail'}].

4 个答案:

答案 0 :(得分:2)

您可以使用ng-class ng-class =&#34; {class1:expression1,class2:expression2}&#34;

在你的情况下是这样的

<div class="progress-bar"
     ng-class="{'progress-bar-success':row.entity.status==='complete', 'progress-bar-warning':row.entity.status==='incomplete', 'progress-bar-danger':row.entity.status==='fail'}">
</div>

这是掠夺者。 https://plnkr.co/edit/ydY6eTFE7PbJ4j8VcM0g?p=preview

答案 1 :(得分:0)

在你的控制器中添加它;

 var myApp = angular.module('myApp');

myApp.controller('clickCtrl', function($scope){

  $scope.dynamicClass = "progress";

});

在您看来:

 cellTemplate :  "<div ng-class="dynamicClass"> <div class="progress-bar progress-bar-info" ></div> </div>"

定义css:

.progress{
    background-color: orange !important;
}


.done{
    background-color: green!important;
}

答案 2 :(得分:0)

你应该使用angulars ngClass指令。

https://docs.angularjs.org/api/ng/directive/ngClass

答案 3 :(得分:0)

如果您确实想要使用自己的样式,请确保不要将它们定义为&#34; .ui-grid-cell-contents&#34;它实际上适用于rowTemplate但不适用于cellTemplate。我已经看过几篇关于此的帖子,它可以为你节省一些麻烦。