7

When i try to compare the string with ng-if from angularjs, i m getting the errors in console.

Below are my data:

value for {{reports[0]}} is:

{"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}

value for {{reports[0].reportType}} is:

REPORT_TOP_SELLERS

Below are the code which throws the Error:

<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'].

Please help.

3
  • brackets binds the model, without brackets it works. Commented Sep 8, 2017 at 5:13
  • Thank you so much for all your response. Commented Sep 8, 2017 at 6:39
  • first to answer that too right answer, still accepted copied answer Commented Sep 8, 2017 at 11:00

6 Answers 6

16

change html code :

<div ng-if="reports[0].reportType == 'REPORT_TOP_SELLERS'">
Sign up to request clarification or add additional context in comments.

1 Comment

Can we use pattern inside ng-if? e.g. ng-if="report type has only alphabetical values" I tried <div *ngIf="itCovPointInfo['COV_GLOBALS']['rcw'] === '/[a-zA-Z0-9]/'"> but doesn't work.
4

Use without the expression, it should be,

if its angular

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

angularjs

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

Comments

3

do not use brackets just use it direct

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

Comments

2

Don't use curly braces

ng-if is already in an angular context so you can just do this

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

double curly braces: {{ }} -> are for interpolation

Comments

2

Don't use curly braces, To only check equality,it should be

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

To check type and equality,it should be

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

Comments

1

Get rid of {{}}. You don't need interpolation, just access the value directly like:

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.