0

When I use a variable in the view (HTML), when should I put it inside curly braces {{}}?

I saw an example like this where an img tag has two directives with the same value, but the first (ng-src) variable should be in curly braces and the second (ng-show) variable doesn't.

const myApp = angular.module('myApp', []);

myApp.controller('appController', ['$scope', $scope => {
  $scope.img = 'https://img1.ak.crunchyroll.com/i/spire4/852ac41bdb5200059504e6e9ce6fc5ea1516289808_medium.png';
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="myApp">
  <div ng-controller="appController">
    <img ng-src="{{img}}" ng-show="img">
  </div>
</body>
Moaaz Bhnas
  • 1,010
  • 7
  • 18
  • 36
  • 1
    Check the documentation. If the documentation says that the directive take an expression, avoid using using interpolation `{{ }}`. In the case of `ng-src`, the documentaion explicitly says use `{{ }}`. If the attribute is not an AngularJS directive, use interpolation. – georgeawg May 18 '18 at 21:08
  • Very helpful. Thank you @georgeawg – Moaaz Bhnas May 19 '18 at 03:20

0 Answers0