Contents
- 1 How do you communicate with a controller and a directive?
- 2 What is the difference between controller and link in directives?
- 3 How does parent / child controller communication work in AngularJS?
- 4 How are scopes defined in an AngularJS controller?
- 5 How does parent and child controller communication work?
How do you communicate with a controller and a directive?
Shared objects. The simplest way to get a “two-way communication” between a controller and a directive is to set-up a shared object between the two via an ‘=’ scope binding.
What is the difference between controller and link in directives?
The link option is just a shortcut to setting up a post-link function. controller: The directive controller can be passed to another directive linking/compiling phase. It can be injected into other directices as a mean to use in inter-directive communication.
What does a controller do in angular?
The controller in AngularJS is a JavaScript function that maintains the application data and behavior using $scope object. You can attach properties and methods to the $scope object inside a controller function, which in turn will add/update the data and attach behaviours to HTML elements.
How does parent / child controller communication work in AngularJS?
Parent-Child Controller Communication. As the name suggests, this mechanism only works between two controllers where one controller (child) is nested inside another controller (parent). Scopes, which represent the context (data available to the view) of the controller in AngularJS, are hierarchal.
How are scopes defined in an AngularJS controller?
Scopes, which represent the context (data available to the view) of the controller in AngularJS, are hierarchal. This means that variables and functions defined on the parent controller are available to child.
Can a child access the parent scope of a controller?
Children can directly access the parent-scope (even modify it), but the parent cannot access it’s children’s scopes. Angular scopes include a variable called $parent (i.e. $scope.$parent) that refer to the parent scope of a controller.
How does parent and child controller communication work?
Parent-Child Controller Communication Variables defined on the parent’s scope are copied to the child’s scope. If a child defines a variable with the same name as a variable on the parent’s scope, the child is effectively shadowing (hiding) the parent-scope variable.