Put custom data to each directive scope

14 views
Skip to first unread message

Ku buś

unread,
May 24, 2016, 6:19:25 AM5/24/16
to AngularJS
Hello,

In my application I have
app.run(function(rootScope)
{
    $rootScope.Config = Configuration;
    $rootScope.User = doLocalStorage;
    $rootScope.EntityEnums = EntityEnums;
    $rootScope.System = System;
});

Thanks to this I can use my Enums and Configuration in any controller and view. The problem is that I dont know how to inject those values to my directives scope. Any solutions?

Regards,
Qba

Sander Elias

unread,
May 24, 2016, 10:22:52 AM5/24/16
to AngularJS
Hi Ku,

Don't use rootscope for that. Make a service that holds this data, and inject those where ever you need them.
You can split it into a couple of services, and create an extra(few) services for the most used ones.

Regards
Sander

Ku buś

unread,
May 25, 2016, 3:58:14 AM5/25/16
to AngularJS
Why I shouldnt use rootscope? Controllers scopes are created from $rootScope. I dont like the service idea because it doesnt solve my problem. I want to have already all my config variables in $scope and not use:
ctor($scope, configService)
{
 $scope.Config1 = configService.Config1;
 $scope.Config2 = configService.Config2;
}

Sander Elias

unread,
May 25, 2016, 4:36:18 AM5/25/16
to AngularJS
Hi Ku,

Here is a sample controller, utilizing an confservice.

function SampleController(configService) {
   
var vm = this;
   
Object.assign(vm,configService); //extend the controller with the properties of the configservice
   
// BTW if you have to support old browsers you might use angular.extend instead of Object.assign
   
// code ....
}

Do yourself a favor and stop using $scope. Read through the styleguide, and the component docs
using $rootScope the way you do it, is the same as using globals. To know why that is bad, google for "why is global scope bad".

Regards
Sander


Reply all
Reply to author
Forward
0 new messages