$new() or $rootScope.new() in angularjs

$new() or $rootScope.new() in angularjs

The Root Scope is creating by using the $rootScope.

Example code.

The below example going to show how to create the root scope using $rootScope.

var parentRoot = $rootScope;
parentRoot.Email = "[email protected]";//assign the email value.

//Used to check the parent email is equal to the given email.
//If return true then broadcast to "hi, your email is valid!! Thank you!!"
//If return true then broadcast to "hi, your email is not valid!! Please try again."
if (expect(parentRoot.Email).toEqual('[email protected]')) {
    parentRoot.$broadcast("Hi, your email is valid!! Thank you!!");
}
else {
    parentRoot.$broadcast("Hi, your email is not valid!! Please try again.");
}

The Child scopes are creating by using the $new() method.
The most of the scopes are created automatically at the time when compiled & executed to HTML page.

If you want to remove the scope from the hierarchy that time we using to $destroy() method.

Example code.

The below example going to show how to create the child root scope using $rootScope or parent scope.

var parentRoot = $rootScope;
var childRoot = parentRoot.$new();//Used the new method to get the child root.
childRoot.Email = "[email protected]"; //Assign to child root email.

//Used to check the child email is equal to the given emails.
//If return true then broadcast to "hi, your email is valid!! Thank you!!"
//If return true then broadcast to "hi, your email is not valid!! Please try again."
if (expect(childRoot.Email).toEqual('[email protected]')) {
    parentRoot.$broadcast("hi, your email is valid!! Thank you!!");
}
else {
    parentRoot.$broadcast("hi, your email is not valid!! Please try again.");
}


ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.