Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular when to use curly brackets

In angular sometimes i have seen curly brackets but some times not.i search a lot but i couldn't find correct question

with curly bracket

ng-src="{{imageSrc}}

without curly bracket

ng-hide="imageSrc"

what i'm asking is why we cannot write ng-hide as

ng-hide="{{imageSrc}} // doesn't work anyway

why there is 2 different syntax for src and hide?

like image 918
while true Avatar asked Jul 17 '26 12:07

while true


1 Answers

It simply depends on the way the directive you are using is "declared".

If the directive has the following declaration:

scope:{
    ngHide: '='
}

then, you don't have to use double mustaches because the directive expects an object

If the directive is declared like the following :

scope:{
    ngMin:'@'
}

then, it expects a value. If your value comes from a javascript variable, then you have to use curly braces to interpolate the string contained into your variable.

EDIT :

It has been a long time since I read angular source code.

I haven't found any source code to prove my point :

ngController which expects a string is declared like the following

var ngControllerDirective = [function() {
  return {
    restrict: 'A',
    scope: true,
    controller: '@',
    priority: 500
  };
}];

https://github.com/angular/angular.js/blob/master/src/ng/directive/ngController.js#L3

ngMaxLength

var maxlengthDirective = function() {
  return {
    restrict: 'A',
    require: '?ngModel',
    link: function(scope, elm, attr, ctrl) {
      if (!ctrl) return;

      var maxlength = -1;
      attr.$observe('maxlength', function(value) {
        var intVal = toInt(value);
        maxlength = isNaN(intVal) ? -1 : intVal;
        ctrl.$validate();
      });
      ctrl.$validators.maxlength = function(modelValue, viewValue) {
        return (maxlength < 0) || ctrl.$isEmpty(viewValue) || (viewValue.length <= maxlength);
      };
    }
  };
};

https://github.com/angular/angular.js/blob/master/src/ng/directive/validators.js#L186

like image 100
Deblaton Jean-Philippe Avatar answered Jul 19 '26 03:07

Deblaton Jean-Philippe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!