AngularJs ng-if指令不起作用?

时间:2016-05-27 15:37:16

标签: html angularjs angularjs-directive

我对AngularJs很新,所以我不确定ngif语法。我有一张桌子,我需要根据<td>的值更改component.status的类。 一切都有效,除了ngif指令

我的阵列:

$scope.components = 
    [
    {type: "Dispacther",        component: "Dispatcher2",  created: "2016-05-20T01:44:56.113", status: "Live"},
    {type: "Mobilizer",         component: "Mobility3",    created: "2016-05-25T07:34:30.019", status: "Down"},
    {type: "Listener",          component: "ADT 22146",    created: "2016-05-20T01:44:56.113", status: "Live"},
    {type: "OutBound Charge",   component: "Billing 92",   created: "2016-05-20T01:44:56.113", status: "Live"},
    {type: "Listener",          component: "22064",        created: "2016-05-20T01:44:56.113", status: "Live"},
    {type: "Dispacther",        component: "Dispacther1",  created: "2016-05-21T00:48:50.433", status: "Warning"}
];

到目前为止我的尝试:

  <tr ng-repeat="component in components">
  <td >{{component.type}}</td>
  <td>{{component.component}}</td>
  <td>{{component.created}}</td>
  <td ng-if="component.status == Live" class="alert-success">{{component.status}}</td>
  <td ng-if="component.status == Warning" class="alert-warning">{{component.status}}</td>
  <td ng-if="component.status == Down" class="alert-danger">{{component.status}}</td>
  </tr>

2 个答案:

答案 0 :(得分:2)

您的代码可以使用,但是您需要使用引号''封装状态值(ng-if =“component.status =='Live'”)

  <tr ng-repeat="component in components">
  <td >{{component.type}}</td>
  <td>{{component.component}}</td>
  <td>{{component.created}}</td>
  <td ng-if="component.status == 'Live'" class="alert-success">{{component.status}}</td>
  <td ng-if="component.status == 'Warning'" class="alert-warning">{{component.status}}</td>
  <td ng-if="component.status == 'Down'" class="alert-danger">{{component.status}}</td>
  </tr>

答案 1 :(得分:2)

===是一个不变量的字符串,为了比较两个元素,请使用value(比较type并比较==)而不是value(仅比较<td ng-if="component.status == Live" class="alert-success">{{component.status}}</td> )。 变化:

<td ng-if="component.status === 'Live'" class="alert-success">{{component.status}}</td>

致:

ng-class

但在你的情况下,我认为你应该使用<td ng-if="component.status == Live" class="alert-success">{{component.status}}</td> <td ng-if="component.status == Warning" class="alert-warning">{{component.status}}</td> <td ng-if="component.status == Down" class="alert-danger">{{component.status}}</td> 。 变化:

<td ng-class="{'alert-success': component.status === 'Live', 'alert-warning':  component.status === 'Warning', 'alert-danger': component.status === 'Down'}">{{component.status}}</td>

要:

ng-class

内部{'alert-success': component.status === 'Live', 'alert-warning': component.status === 'Warning', 'alert-danger': component.status === 'Down'}response.setContentType("image/jpeg");