angularjs中美元($)符号的用途是什么 [英] what is the use of a dollar ($) sign in angularjs
问题描述
我在此处找到了类似的问题,但我要么没有不能理解答案,或者可能不是完全相同的问题.
I found a similar question here but either i don't understand the answers or maybe it's not exactly the same question.
在 components 的angularjs指南中,有多个示例,其中$符号是在ctrl变量之前使用,如下所示:
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:
Name: {{$ctrl.hero.name}}<br>
在heroDetails控制器中,ctrl变量前没有$符号.如果我删除heroDetails.html中的$符号,它将不再起作用.
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.
有人可以向我解释吗?
预先感谢您的回答.
推荐答案
controller
属性是对控制器实例的引用,该实例将由组件使用.通过使用controllerAs
属性在视图中声明控制器的名称.如果您未声明controllerAs
,则默认值为$ctrl
.因此,$符号只是名称的一部分.
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.
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: '&'
}
});
这篇关于angularjs中美元($)符号的用途是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!