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?
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.
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.
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.
The three types of directives in Angular are attribute directives, structural directives, and components.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With