Search code examples
angularjsdependency-injectionangular-directiveangular-controller

Accessing directive from a different module


I have two different modules , first one contains controller second directive.

Basically i want the directive to be rendered in my view. But as modules for directiveandcontroller are different, its giving error.

If i give same module then it ressolves my issue by i want a generic module serving all controllers.

controller

angular.module('SlamModuleCtrl')
        .controller('SlambookExt',['$scope','$route','SlambookCollection', function($scope,$route) {
            console.log("slam controller")
        }]);

Directive

angular.module('Genericmodule')
    .directive('appVersion', ['version', function (version) {
       return {
    restrict: 'E',  
    template: '<h1>version1</h1>'
    } 
    }])

View

 <div ng-controller="SlambookExt">
     <app-version>1</app-version>
    </div>

Solution

  • When instantiating slamModuleCtrl, you need to specify genericModule as a dependency.

    Or use a parent module that loads both those modules as dependencies and use that parent module as your ng-app.

    angular.module('parentModule',['slamModuleCtrl','genericModue'])

    This is just a plausible solution because both your modules look right to me. So I'm guessing that the version is not showing up because the module hasn't been loaded