Angulajs how to share data between controllers

Source: Internet
Author: User

When you set $scope data in a controller to affect the scope of other control, you can use $rootscope

As an example:

Set within a control a range

Mainapp.controller (' Menucontroller ', function ($scope, $rootScope) {
$rootScope. menus={}
$rootScope. Menus.showwelcome = false;
$rootScope. Menus.showexit = false;
$rootScope. Menus.activehome = "active";
$rootScope. Menus.activemystudy = "";
});

In another control B

Mainapp.controller (' HomeController ', function ($scope) {
$scope. Menus.activehome = "active";
$scope. Menus.activemystudy = "";
$scope. LoginForm = {userid: "Lee"};
$scope. Login = function () {
$scope. Menus.loginid = $scope. Loginform.userid;
$scope. Menus.showwelcome = true;
$scope. Menus.showexit = true;
}
});

When you manipulate the elements in B, the elements in a are affected. Note that if the data name in B is the same as in $rootscope, you need to use $rootscope when referencing in B.

The key factor is that all applications have a $rootScopethat can function in all of the HTML elements contained in the NG-APP directive.

$rootScope can act on the entire application. is the bridge of scope in each controller. Values defined with Rootscope can be used in each controller.

Angulajs how to share data between controllers

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.