1

how can I reuse a simple angular component via Dependency injection?

Component:

angular.module("navbar", []).component("nav", function() {
    console.log("component loaded");
});

Other Module Controller I want to use it in:

angular.module('CtrlHome', ['navbar']).controller('HomeController', 
function($rootScope, $scope) {
});

And finally use it in the template

<nav></nav>

It throws me a "injector modulerr" error. What am I doing wrong here?

1

1 Answer 1

1

The component isn't defined correctly. The component registration method takes a string and an object as input. The object describes the component. So this would be the correct way to register a component with the module:

angular.module("navbar", []).component("nav", {
    controller: function(){
        console.log("component loaded");
    }
});

There are several other properties you can include to describe a component, i.e. bindings: {} and template: '<div>Hello, World!</div>'.

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

6 Comments

Can you provide jsfiddle or snippet?
thanks for the quick response, I think you accidently put a squared bracket. i'm still getting the same error though after changing it .
Yes, good catch. That's a brace :-) If you still get the same error, perhaps your navbar module isn't registered with angular before the CtrlHome is registered. Try checking they're loaded in the correct order.
As long as modules are used like angular.module('CtrlHome', ['navbar']), loading order doesn't matter. The error means that the module wasn't loaded at all at bootstrap time.
Ups well registering it in the index.html helps alot ... ive worked with webpack for too long ... thanks anyways
|

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.