angular 何时使用大括号 [英] angular when to use curly brackets

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

问题描述

在 angular 中,有时我看到大括号,但有时没有.我搜索了很多,但找不到正确的问题

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".

如果指令有以下声明:

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.

好久没看 angular 的源代码了.

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/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

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

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