Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs Controller destructor

I have an AngularJs app. I use Controllers for some child scopes. In every Controller I can set a number of variables that belong to the corresponding Child Scope. When AngularJs instantiate a controller, there is a constructor where I can set a default value to my child-scope variables.

Do I have a controller "destructor"? How do I know when a controller is closing and the scope is being cleaned (destroyed by the $destroy function)?

Thanks!

like image 612
Rafa Avatar asked Jul 07 '14 09:07

Rafa


People also ask

What are the controllers in AngularJS?

AngularJS Controllers AngularJS applications are controlled by controllers. The ng-controller directive defines the application controller. A controller is a JavaScript Object, created by a standard JavaScript object constructor.

Can we create nested controllers in AngularJS?

Nested Controllers: AngularJS allows using nested controllers. It means that you have specified a controller in an HTML element which is a child of another HTML element using another controller.

Which is the correct syntax of creating AngularJS controller?

13) Which of the following syntax is used to create a module in AngularJS? Answer: C is the correct option. To create a module in AngularJS, we use angular. module("app", []); syntax.

What is module and controller in AngularJS?

An AngularJS module defines an application. The module is a container for the different parts of an application. The module is a container for the application controllers. Controllers always belong to a module.


1 Answers

You have to listen to the $destroy event, e.g.:

function MyController($scope, ...) {     ...     $scope.$on("$destroy", function handler() {         // destruction code here     }); } 

Relevant docs: https://docs.angularjs.org/api/ng/type/$rootScope.Scope

like image 59
Nikos Paraskevopoulos Avatar answered Sep 18 '22 07:09

Nikos Paraskevopoulos