使用花括号时的角度 [英] angular when to use curly brackets

查看:104
本文介绍了使用花括号时的角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时在尖角处我会看到花括号,但有时却没有.我搜索了很多,但找不到正确的问题

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

带有花括号

ng-src="{{imageSrc}}

不带花括号

ng-hide="imageSrc"

我要问的是为什么我们不能写ng-hide

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

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

为什么srchide有两种不同的语法?

why there is 2 different syntax for src and hide?

推荐答案

这完全取决于声明"所使用指令的方式.

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

如果指令的声明如下:

scope:{
    ngMin:'@'
}

然后,它期望一个值.如果您的值来自javascript变量,则必须使用花括号将包含在变量中的字符串插值.

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.

自从我阅读有角度的源代码以来已经很长时间了.

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

我还没有找到任何源代码来证明我的观点:

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

ngController希望像下面这样声明一个字符串

ngController which expects a string is declared like the following

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

https://github.com. 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. com/angular/angular.js/blob/master/src/ng/directive/validators.js#L186

这篇关于使用花括号时的角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆