Angularjs Controller inherits from another controller _angularjs

Source: Internet
Author: User

Angularjs controller inheritance, commonly used is scope nesting scope. By default, when a property cannot be found in the current scope, it is searched in the parent scope, if not found until $rootscope.

But in some cases, Rootscope is our controller, it is impossible to write a large number of common property methods to Rootscope.

For example, there are a number of similar pages, there are crumbs, search bar, toolbars, tables and other elements, crumbs form such elements considered as directive, then there will be many similar configurations need to be transferred from controller to the component, there will be a lot of tool-class methods for processing data, It is obviously difficult to write the same code repeatedly in the controller of each page, and you need to use inheritance.

Found a solution on the StackOverflow, the original angularjs has taken into account this situation, providing a $controller

var app = Angular.module (' Angularjs-starter ', []); 
App.controller (' Parentctrl ', function ($scope) {
//I ' m the sibling, but want to act as parent
});
App.controller (' Childctrl ', function ($scope, $controller) {
$controller (' Parentctrl ', {$scope: $scope});//this Works
});

The above is a small set to introduce the Angularjs controller inherited from another controller related knowledge, hope to help everyone!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.