user2598808
user2598808

Reputation: 643

Use a constant value in html file

Can anyone tell me how to pass a constant value to html in AngularJS? Example: In html file, I have an ng-if condition.

<span ng-if="prod.ID">10</span>

Here I want to make the value "10" as configurable. Something like :

<span ng-if="prod.ID == constants.prodID">10</span>

Any suggestions?

Thank You in advance.

Upvotes: 0

Views: 5766

Answers (2)

Norbor Illig
Norbor Illig

Reputation: 658

First, create a constant:

   app.constant("ProductConst", {
      "prodTotal": 10
   });

Second, inject it into your controller

app.controller('MyController', function ($scope, ProductConst) {
    $scope.productTotal = ProductConst.prodTotal;
}

Last, use it in your view:

<span  ng-if="prod.ID > productTotal "></span>

Upvotes: 4

Yoeri
Yoeri

Reputation: 2249

  1. inject the constant in your controller
  2. put the constant on the $scope

That should do the trick :-)

Upvotes: 3

Related Questions