Search code examples
angularjsangularjs-serviceangularjs-components

Add angularjs component via service


I am trying to add angularjs component using the code as per below. app.component doesn't work like this

Where as if I execute app.component from outsite it works.

How to fix this issue. Notice that the API will return component names. I just need to render them.

app.service('applookup', function($http) {
    this.register = function() {

        return $http.get('http://localhost:3003/api/provider/fetch/app1').then(function(res){
            app.component('appleComponent', {
                template : 'test'
            });
            return res.data.componentList;
        });
    }
});

Solution

  • As @William and @Zooly mentioned in comments. We can add reference to $compileProvider.component as per below in app.config

    app.register = {
    
    component :  function(name, object) {
        $compileProvider.component(name, object);
    return (this);
    }
    

    Then use app.register.component to register the component