Preface
Everyone knows that by default, directives should access the parent scope. If we expose the scope of the parent controller to the directive, the directive can freely modify the scope attribute. In some cases, your directive may want to add some properties and functions that can only be used internally. If we all complete them in the parent scope, it may pollute the parent scope. Therefore, we have the following two options:
Use parent scope - If you do not need to operate the parent scope attribute and do not need a new scope, you can directly use the parent scope.
scope:false
A child scope - This scope will prototype inherit the parent scope
scope:true
An isolated scope - a brand new, non-inherited, independent scope
scope:{}
Scopes can be defined by directive definition objects in scope attributes. Here are some instructions about scope attributes:
Common types of scope in directives
=
- '=', used for two-way binding of child scope and parent scope. Using this method, you can assign an actual scope model to an attribute instead of a normal string. The effect is that you can pass complex data models, such as arrays/objects, etc. to isolation scope. If the parent scope or child scope attribute is changed, it will affect the other party accordingly.
- '=?',This situation can avoid this behavior using `=?` or `=?attr` in order to flag the property as optional.'
- '=*',If you want to shallow watch for changes (. $watchCollection instead of $watch) you can use `=*` or `=*attr` (`=*?` or `=*?attr` if the property is optional).
&
- '&', used to execute functions in the parent scope.
@
- '@', perform single-item text binding. Use this method to pass strings to attributes. When the parent scope attribute changes, the isolation scope model also changes. However, the opposite does not hold true! You cannot change the parent scope by manipulating the isolation scope.
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.