Explain several special cases of scope type in Angular. js instruction in detail

  • 2021-07-22 08:46:57
  • OfStack

Preface

Everyone knows that by default, The instruction should access the parent scope. If we expose the scope of the parent controller to the instruction, Then the instruction is free to modify the scope properties. In 1 case, your instruction may want to add 1 properties and functions that can only be used internally. If we all do it in the parent scope, it may pollute the parent scope. Therefore, we have the following two choices:

Use Parent Scope-If you do not need to manipulate the Parent Scope property and do not need a new scope, you can use the Parent Scope directly

scope:false

1 child scope-this scope inherits the parent scope

scope:true

1 Isolated Scope-1 completely new, non-inherited, standalone scope

scope:{}

Scope can be defined by the scope attribute in the instruction definition object. Here is a description of the scope attribute:

Common types of scope in instructions

=

'=' for bi-directional binding of child and parent scopes. This method allows you to assign an actual scope model to an attribute, Instead of an ordinary string. The effect is that you can pass complex data models, such as arrays/objects, to isolated scopes. Parent or child scope properties change, which will affect each other accordingly. '=? 'This situation prevents the current attribute from being present in the parent scope attribute and from throwing an exception.--' If the scope property doesn 'ES40exist, it will throw a NON_ASSIGNABLE_MODEL_EXPRESSION exception. You can avoid this using ` =? ` or ` =? attr ` in order to flag the property as optional. ' '=*',If you want to shallow watch for changes (i.e. $watchCollection instead of $watch) you can use `=*` or `=*attr` (`=*?` or `=*?attr` if the property is optional).

&

' & ', used to execute a function in the parent scope.

@

'@', single text binding. This method allows you to pass a string to a property, and when the parent scope property changes, the isolation scope model also changes. However, the reverse is not true! You cannot change the parent scope by manipulating the isolation scope.

Summarize


Related articles: