如何在角度js中应用ng-if

时间:2016-07-28 13:06:20

标签: javascript angularjs

我正在从表单中的服务中检索数据。

[["Sql Injection",0],["Proxy Abuse",0],["Spam",0],["Information and Source Code Leakage",0],["System Command Injection",0],["Cross-Site Request Forgery",0],["Session Hijacking",0],["PHP Injection",0],["Request Anomaly",0],["Local/Remote File Inclusion",0],["Cross-Site Scripting",0]].

现在我希望如果每种攻击类型的计数为零,则不显示任何内容。我如何通过ng-if来处理这个问题。 模板代码:

<div class="col-md-6 b-r b-light no-border-xs" ng-show="webSummary[0]">
    <highchart id="chart1" config="webConfig" class="span9" ></highchart>           
</div>

2 个答案:

答案 0 :(得分:2)

你可以这样做:

angular
  .module('MyApp', [])
  .controller('MyController', function($scope) {
    $scope.webSummary = [["Sql Injection",0],["Proxy Abuse",0],["Spam",0],["Information and Source Code Leakage",0],["System Command Injection",0],["Cross-Site Request Forgery",0],["Session Hijacking",0],["PHP Injection",0],["Request Anomaly",0],["Local/Remote File Inclusion",0],["Cross-Site Scripting",0]];
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="MyApp" ng-controller="MyController">
  <div class="col-md-6 b-r b-light no-border-xs"  
       ng-repeat="ws in webSummary"
       ng-if="ws[1]">
    <highchart id="chart1" config="webConfig" class="span9"></highchart>
  </div>
</div>

答案 1 :(得分:1)

你可以做到

public abstract class TypeToken<T> {
    private final AnnotatedType type;

    /**
     * Constructs a type token.
     */
    protected TypeToken() {
        this.type = extractType();
    }

    private TypeToken(AnnotatedType type) {
        this.type = type;
    }

    public Type getType() {
        return type.getType();
    }

    public AnnotatedType getAnnotatedType() {
        return type;
    }

    private AnnotatedType extractType() {
        AnnotatedType t = getClass().getAnnotatedSuperclass();
        if (!(t instanceof AnnotatedParameterizedType)) {
            throw new RuntimeException("Invalid TypeToken; must specify type parameters");
        }
        AnnotatedParameterizedType pt = (AnnotatedParameterizedType) t;
        if (((ParameterizedType) pt.getType()).getRawType() != TypeToken.class) {
            throw new RuntimeException("Invalid TypeToken; must directly extend TypeToken");
        }
        return pt.getAnnotatedActualTypeArguments()[0];
    }
}

在控制器中

<div class="col-md-6 b-r b-light no-border-xs" ng-if="checkAttackCount()">
    <highchart id="chart1" config="webConfig" class="span9" ></highchart>           
</div>