📜  AngularJS前缀$和$$如何使用?

📅  最后修改于: 2021-05-13 19:13:27             🧑  作者: Mango

  • $:

    AngularJs中的$是一个内置对象,它包含应用程序数据和方法。

    scope($)充当控制器和视图之间的链接。

    范围($)

    在控制器函数内部,可以附加scope($)的属性和方法。可以使用Expressionng-modelng-bind指令在视图中显示范围数据。

    
    
    
        
    
    
    

    $scope

        
            Message:
            {{message}}
            
                  
        

    输出:

    范例2:

    
    
        
        
            
                First Name:
                Middle Name:
                Last Name:
                
             Full Name: {{firstName +middleName+ " " + lastName}}         
                    

    输出:

    $ rootScope:

    AngularJS应用程序由单个$ rootScope组成。其他所有$ scope都是子对象。$ rootscope具有附加的属性和方法,将对所有控制器可用。

    Method

    Description

    $new It is used to create new Child Scope
    $watch It is used to Register a callback which is to be executed whenever the model property changes.
    $watchGroup

    It is used to register a callback  which is to be executed whenever model properties changes.

    We specify an array of properties in this.

    $watchCollection It is used to register a callback which is to be executed whenever model object or array property changes.
    $digest It processes all of the watchers of the current scope and its children.
    $destroy Remove the current scope from parent scope.
    $eval Execute expressions on the current scope.
    $emit It is used to dispatch the specified event upwards till $rootScope.
    $broadcast It is used to dispatch the specified event downwards till child Scope.

    范例3:

    
    
    
        
    
    
    

    $watch

        
            Enter Message:
            New : {{newMessage}}
            Old : {{oldMessage}}      
        

    输出:

  • $$

    其中的$$被视为私有变量。我们使用$$来避免内部变量冲突,并且不暴露给外部使用。

    下面列出了其中一些:

    $$观察者,$ watchers,$ childHead,$ childTail,$ ChildScope等。

    Method

    Description

    $$watchers It contains all of the watches associated with the scope
    $$asyncQueue It is a async task queue.It is consumed on every digest
    $$postDigest(fn) It executes fn after the next digest cycle.
    $$destroyed It destroys the scope.

    句法:

    $$('.selector');

    或者

    element.all(by.css('.selector'));

    借助AngularJS核心功能,应用程序模块之间的常见通信方式是使用$ parent,$ childHead,$ nextSibling建立控制器之间的连接。

    例子:

    
    
        
        
            
                First Name:
                Middle Name:
                Last Name:
                
           Full Name: {{firstName +middleName+ " " + lastName}}         
                    

    输出:

    当我们添加$$时,将产生上述输出,因为它充当私有对象。

因此,为防止在编写代码时出现意外的名称冲突,请在公共对象前添加$角前缀,在$$之前添加私有对象。