将字符串与ng-if angularjs进行比较

时间:2017-09-08 05:09:21

标签: angularjs

当我尝试将字符串与angularjs中的ng-if进行比较时,我在控制台中收到错误。

以下是我的数据:

{{reports[0]}}

值为:

{"name":null,"discount":0,"margin":0,"reportType":"REPORT_TOP_SELLERS","revenue":0,"variantKey":null,"rank":1,"productKey":"10692-1_en_US","purchasedUnits":0,"abandonedUnits":0}
{{reports [0] .reportType}}的

值为:

REPORT_TOP_SELLERS

以下是抛出错误的代码:

<div ng-if="{{reports[0].reportType}} === 'REPORT_TOP_SELLERS'">

Error:
Error: [$parse:syntax] Syntax Error: Token '{' invalid key at column 2 of the expression [{{reports[0].reportType}} === 'REPORT_TOP_SELLERS'] starting at [{reports[0].reportType}} === 'REPORT_TOP_SELLERS'].

请帮忙。

6 个答案:

答案 0 :(得分:5)

更改HTML代码:

<div ng-if="reports[0].reportType == 'REPORT_TOP_SELLERS'">

答案 1 :(得分:3)

不要使用括号直接使用它

<div ng-if="reports[0].reportType === 'REPORT_TOP_SELLERS'">

答案 2 :(得分:3)

在没有表达式的情况下使用,应该是,

如果 angular

<div *ngIf="reports[0].reportType === 'REPORT_TOP_SELLERS'">

<强> angularjs

<div ng-if="reports[0].reportType === 'REPORT_TOP_SELLERS'">

答案 3 :(得分:1)

不要使用花括号

ng-if已经在角度上下文中,所以你可以这样做

<div ng-if="reports[0].reportType === 'REPORT_TOP_SELLERS'">

双花括号:{{ }} ->用于插值

答案 4 :(得分:1)

摆脱{{}}。您不需要插值,只需直接访问该值,如:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.reports = [{"name":null,"discount":0,"margin":0,"reportType":"REPORT_TOP_SELLERS","revenue":0,"variantKey":null,"rank":1,"productKey":"10692-1_en_US","purchasedUnits":0,"abandonedUnits":0}];
    
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
  <div ng-if="reports[0].reportType === 'REPORT_TOP_SELLERS'">
    Report Type is 'REPORT_TOP_SELLERS'
  </div>
</div>

答案 5 :(得分:1)

不要使用花括号, 要仅检查相等性,它应该是

<div ng-if="reports[0].reportType == 'REPORT_TOP_SELLERS'">

要检查类型和相等性,它应该是

 <div ng-if="reports[0].reportType === 'REPORT_TOP_SELLERS'">