什么是美元($)签到angularjs

时间:2022-05-01 19:37:07

I found a similar question here but either i don't understand the answers or maybe it's not exactly the same question.

我在这里发现了一个类似的问题,但要么我不理解答案,要么可能不是完全相同的问题。

In the angularjs guide on components there are multiple examples of views where a $ sign is used before a ctrl variable like in the following:

在组件的angularjs指南中,有多个视图示例,其中在ctrl变量之前使用$符号,如下所示:

Name: {{$ctrl.hero.name}}<br>

In the heroDetails controller, there is no $ sign before the ctrl variable. If I remove the $ signs in the heroDetails.html it does't work anymore.

在heroDetails控制器中,ctrl变量之前没有$符号。如果我删除了heroDetails.html中的$符号,它就不再起作用了。

Can anybody explain it to me, please ?

有人可以向我解释一下吗?

Thanks in advance for your answers.

提前感谢您的回答。

2 个解决方案

#1


2  

The controller property is the reference to controller instance, that will be used by the component. The name of the controller in the view is declared by using the controllerAs property. If you don't state controllerAs, the default is $ctrl. So the $ sign is just a part of the name.

controller属性是对控制器实例的引用,将由组件使用。视图中控制器的名称是使用controllerAs属性声明的。如果您没有声明控制器,则默认为$ ctrl。所以$符号只是名称的一部分。

angular.module('heroApp').component('heroDetail', {
  templateUrl: 'heroDetail.html',
  controller: HeroDetailController, // the controller class
  controllerAs: 'whatever', // the controller alias in the view - default $ctrl
  bindings: {
    hero: '<',
    onDelete: '&',
    onUpdate: '&'
  }
});

#2


2  

When you make a Component, its controller is identified in the view as $ctrl.

制作Component时,其控制器在视图中标识为$ ctrl。

It's referenced in the Component documentation:

它在组件文档中引用:

                   -------------------------------------------------
                  |        Directive        |       Component       |
    --------------|-------------------------|-----------------------| 
   | controllerAs | Yes (default: false)    |  Yes (default: $ctrl) |
    ----------------------------------------------------------------

The symbol $ is the prefix used by Angular for its own variables/properties.

符号$是Angular用于其自己的变量/属性的前缀。

#1


2  

The controller property is the reference to controller instance, that will be used by the component. The name of the controller in the view is declared by using the controllerAs property. If you don't state controllerAs, the default is $ctrl. So the $ sign is just a part of the name.

controller属性是对控制器实例的引用,将由组件使用。视图中控制器的名称是使用controllerAs属性声明的。如果您没有声明控制器,则默认为$ ctrl。所以$符号只是名称的一部分。

angular.module('heroApp').component('heroDetail', {
  templateUrl: 'heroDetail.html',
  controller: HeroDetailController, // the controller class
  controllerAs: 'whatever', // the controller alias in the view - default $ctrl
  bindings: {
    hero: '<',
    onDelete: '&',
    onUpdate: '&'
  }
});

#2


2  

When you make a Component, its controller is identified in the view as $ctrl.

制作Component时,其控制器在视图中标识为$ ctrl。

It's referenced in the Component documentation:

它在组件文档中引用:

                   -------------------------------------------------
                  |        Directive        |       Component       |
    --------------|-------------------------|-----------------------| 
   | controllerAs | Yes (default: false)    |  Yes (default: $ctrl) |
    ----------------------------------------------------------------

The symbol $ is the prefix used by Angular for its own variables/properties.

符号$是Angular用于其自己的变量/属性的前缀。