简体   繁体   中英

Using father scope variables in angular.js

I am new to angular.js and I am trying to understand something.

Let's say I have a DB of companies and employees and I am creating a structure to retrieve all the companies and the employees in the first page (index.html - I am using the:

$routeProvider.when('/index', {
      templateUrl: 'partials/companies-list.html',
      controller: 'AppCtrl'
    })

In this controller I fetch from DB all the companies.

On click on one of the companies I am routing to another view with another controller:

.when('/group/:id', {
      templateUrl: 'partials/company-employees.html',
      controller: 'CompanyEmployeesInfo'
    })

I already have all the data I need in the AppCtrl about the company and I don't want to fetch it again from DB.

Is it possible to use Data from another scope, or pass the data so I won't need to bring it again from my servers?

I really appreciate your help, Eric

您可以将数据存储在$rootScope -“全局”可用

You can create a service which serves as a State - Manager, which stores the data of the particular company into something like current_company. This can be an array or JSON, of your choice.

Remember this would happen just before you route to the employees page.

In 'CompanyEmployeesInfo' you just need to access the get method of this service to retrieve the information of the current company selected.

Please Note: The set and get methods will have to be explicitly defined and returned from the service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM