Your current code structure involves the destruction and recreation of elements upon clicking the "change index" button:
that.changeIndex = function(){
that.arr = [{name : g++}];
that.isValid = !that.isValid;
}
ng-repeat
initiates the destruction and recreation of your myCon2
controllers when creating a new arr
array:
<div ng-repeat="m in con.arr track by m.name" ng-controller="myCon2 as con1">
Similarly, ng-if
triggers the destruction or recreation of your my-dir
directive upon changing the value of isValid
:
<div my-dir ng-if="con.isValid">hey directive!!!!</div>
Both the controller and directive are set to execute a handler function upon document ready, ensuring it runs smoothly even if the code loads after the initial readiness state. Although using document ready is common practice, it's not necessary within Angular controllers and directives. Initialization code can be placed at the beginning of these components or utilize ng-init
.