1

Is it possible to evaluate an angular expression in a ng-if?

For instance:

<div ng-if="itemData.contentTypeId = {{$root.res('resources', article_type_id')}}">
<!-- some content -->
<div>

also tried surrounding the expression in single quotes:

<div ng-if="itemData.contentTypeId = '{{$root.res('resources', article_type_id')}}'">
<!-- some content -->
<div>

and neither worked as expected.

Is this an issue with syntax or is this not possible? If this is a syntax issue, how should the statement above be written?

1
  • 2
    have you tried with == instead of = ? Commented Apr 27, 2015 at 18:21

1 Answer 1

4

The ng-if text IS an angular expression. Whatever string you write in there gets parsed by angular using $scope.$eval(). Any variables (including functions) that are used in the expression must be visible from the current $scope. That means for this code to work, $scope.$root must be defined. Make sure it is, or you can't run the res() function, or else find another way.

ng-if evaluates the expression to a truthy or falsey value, so your = must be a comparison operator == or ===.

<div ng-if="itemData.contentTypeId === $root.res('resources', article_type_id')">

Sign up to request clarification or add additional context in comments.

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.