21)what is $on ?
$on function in angular is used to listen the event that has been triggered.It listens the events that are triggered by using $broadcast and $emit functions.$on is one of the function of $scope which can be initialized like $scope.$on
syntax :
app.controller('ctrlname', function($scope){
$scope.$on('emitorbroadcast', function(){
//our code
})
});
22)How can you create your own directive ?
By using custom directives we can create our own directives with certain behavior adding to it. That created directive can be used as an markup element in our view.
syntax:
myApp.directive(‘ourdirective’, function() {
return {
restrict: ‘E’,
template: ‘<h1>I made a directive!</h1>’
};
});
Using in view :
<ourdirective> </ourdirective>
23)What is link ?
While bootstraping the angular, the compiler cant get perform actions over the scope members. To get start working over them it should pass over the link phase where the scope values get initialised, ofcourse we get the compile phase before getting to this step but the instance of our directives template will gets generated at link itself.If we define the link key with its function as value then it executes as like the post-link by default.
24)What is compile ?
In the process of bootstrap, the parser gets hit to the $compile function where this function grabs all the templates related to the directives. At this phase we cant play with the scope for the templates as the instance will not be created.The compile phase will gets compiled only once for the template and the impact of its logic will apply to all created instances later.The return type of the compile phase will be as pre and post functions.
25)What is the order of execution between pre, post and compile ?
- Initially, the compile phase will gets executed which returs pre/post functions.
- After compile phase getting executed, the pre-link will execute.
- As soon as the pre-link executed goes to the post functions execution flows from bottom-top trace over the custom directive concern template.
26)What is pre ?
In pre-link function we won't get access towards the child elements which we careated by the directives.In some cases we may have to use the parent and child elements as custom directives. At that situation for some of the child elements(created via directive) scope values can make enable at the pre-link function itself as they execute firstly then moves to post while parsing.
27)What is post ?
Post function in the custom directives is mostly preferrred place to write any kind of logic over the template as the parent & child, both elements with its template related instance and scope will be available.
For more details related to the pre and post find in the followed link :
28)What is restrict ?
Restrict is used as one of the key in the custom directives. By using this key we can make or set the behavior of our directive which we are creating. The restrict can be of four modes as follows :
E - Restrict with value as "E" will behaves as an element in our view.
A - Restrict with values as "A" will behaves as an attribute for an element in our view.
C - Restrict with values as "C" will acts as a class for an element.
M - Restrict with values as "M" will acts as a comment.
29)Does angular allows nested controllers using ?
Yes, we can also use the nested controllers based on the requirement.
30)Can we go for $watch in the compile phase ?
No, as compile is the initial phase over the directive, It just creates the template for the directive while parsing over the DOM. So, we can't access the scope members. We can play over $watch in the post function.