Reuse AngularJS Component in another Module (Dependency Injection)

北战南征 提交于 2021-02-08 07:30:10

问题


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:


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>'.



来源:https://stackoverflow.com/questions/46715053/reuse-angularjs-component-in-another-module-dependency-injection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!