After studying various different projects and reading as much documentation as I can handle, I've run into an issue with how to include directives in my app. The app is setup like the following:
app.js - just the top part
angular.module('ngDashboard', ['ngCookies','ngResource','ngDashboard.filters', 'ngDashboard.services', 'ngDashboard.directives'])
All of the modules work fine except (it is an app rewritten from an example) for the directives which don't work at all:
directives.js - The following does NOT work and doesn't execute the directive on the view:
angular.module('ngDashboard.directives', []).
directive('funkyElement', function () {
return {
restrict: 'E',
transclude: true,
scope: 'isolate',
template: '<div>gonna parse this: {{orig}} <br/>... and get this: {{obj}}</div>',
//templateUrl: 'template.html',
compile:function (element, attr, transclusionFunc) {
return function (scope, iterStartElement, attr) {
var origElem = transclusionFunc(scope);
var content = origElem.text();
scope.orig = content;
scope.obj = my_custom_parsing(content);
};
}
};
});
The following in the same directives.js file does work properly and the directive executes:
angular.module('ng').directive('funkyElement', function () {
return {
restrict: 'E',
transclude: true,
scope: 'isolate',
template: '<div>gonna parse this: {{orig}} <br/>... and get this: {{obj}}</div>',
//templateUrl: 'template.html',
compile:function (element, attr, transclusionFunc) {
return function (scope, iterStartElement, attr) {
var origElem = transclusionFunc(scope);
var content = origElem.text();
scope.orig = content;
scope.obj = my_custom_parsing(content);
};
}
};
});
The HTML is simple:
<funky-element>
Parse me... come ooooon! Just parse meee!
</funky-element>
My question is, what is the proper way to include a set of directives and perhaps why the first example (using the ngDashboard.services) does not work.
It turns out that the app.js file I had was either cached so the directive dependency wasn't there or I had neglected to save it (both possible with weekend work and late nights). Since this issue was fixed after I made an update to app.js I'll mark this as resolved with the advice of:
Lastly with regards to Angular, I had not realized you could add directives to the ng
module and they'd become available. I'm sure this isn't a best practice, but for testing and putting together quick code, it may come in handy.
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