Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS use constant in directive

I have defined a constant like this:

app.constant('ngSettings', {
    apiBaseUrl: 'https://url/'
});

how can I reference now this constant in a directive?

Where the directive is something like this:

angular.module('my.directive', []).directive(...................
like image 838
David Dury Avatar asked Oct 15 '25 18:10

David Dury


1 Answers

You can inject the constant anywhere that is injectable including the directive definition itself, the controller of a directive, the link function, et. al.

angular.module('my.directive', []).directive('name', ['ngSettings', function (ngSettings) {
    // do things with ngSettings
    return {};
}]);

By the way I wouldn't name anything you define yourself as ng -- that should be preserved for things in the ng module or that Angular itself created.

like image 114
Explosion Pills Avatar answered Oct 18 '25 12:10

Explosion Pills



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!