Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a directive from module in Angular

We are trying to figure out if there is a way to remove/replace/override a set of directives temporarily whilst in a 'preview' mode.

We have tried removing the module(s) that the directives are contained in, e.g.:

angular.module('myModule', []);

but the directives are still active.

Can anyone help?

like image 735
james.cookie Avatar asked May 24 '13 12:05

james.cookie


People also ask

Is module a directive in Angular?

Modules in Angular provide many benefits. For now, let's focus on the ability to declare a directive in a module using the declarations property of the metadata; that value of which is an array of classes that are the directives that we are using in the module.

What is the difference between component and directive in Angular?

The Component is used to break up the application into smaller components. That is why components are widely used in later versions of Angular to make things easy and build a total component-based model. The Directive is used to design reusable components, which are more behavior-oriented.

What is attribute directive in Angular?

The attribute directive changes the appearance or behavior of a DOM element. These directives look like regular HTML attributes in templates. The ngModel directive which is used for two-way is an example of an attribute directive.

How many types of directives are there in Angular?

The three types of directives in Angular are attribute directives, structural directives, and components.


2 Answers

Internally AngularJS creates factories from directives, by adding a suffix to the directive name. So you can disable the directives by replacing the factories with noop factories.

var noopDirective = function() { return function () {}; };
if (previewMode) {
    // Disable ngPaste directive
    angular.module('myModule')
        .factory('ngPasteDirective', noopDirective);
}

Make sure this is the last executed code.

like image 50
guzart Avatar answered Sep 21 '22 22:09

guzart


As an alternative, consider putting a preview mode into each of the directives. Pass in an attribute to indicate whether the current state is preview or 'live', and conditionalize the directive template with ng-switch.

Tastes vary, but that feels like a more legible approach to me than redefining the directives on-the-fly.

like image 34
S McCrohan Avatar answered Sep 24 '22 22:09

S McCrohan