0

I have created a directives and while I call that directives, I need to load my JS first. Is there anyway to inject Javascript directly into Directives of angularjs?

Here is the code.

var directive = module.exports = function($rootScope, $location, service, user) { return { restrict: 'E', link: function () { $rootScope.$on( '$routeChangeSuccess', function(event,current) { var path = $location.path(); var pageName = current.$$route.title ? current.$$route.title : path; if(!current.redirectTo) { user.then(function(userInfo) { service.method(pageName, { marketName: userInfo.marketName, accountName: userInfo.name, clientServices: userInfo.isClientServices(), userType: userInfo.type }); }); } }); } }; };

directive.$inject = ['$rootScope', '$location', 'service', 'user'];

Please do needful.

1
  • Can you please give an tiny example? Because, if it is plain JS, you just copy it inside the directive's controller Commented Nov 13, 2014 at 9:10

1 Answer 1

0

I dont think you can bundle a directive with js code (like web components), the js has to be included outside of the directive. You can however use angularjs dependency injection to inject the included angular services to your directive:

app.factory('myDependency', function() {
  var myDependencyInstance;
  return myDependencyInstance;
});

app.directive('directive', ['myDependency', function(myDependency) {
     return {
      restrict: 'E',
      templateUrl: 'my-customer.html'
    };
}])

But I guess its not what you want. You could bundle js into the directive's template, but then the js code would be duplicated every time you add the directive and this would propably only work with non angular js code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.