How can I access AngularJs component templateUrl variable inside of a controller $init fucntion?

86 Views Asked by At

Good morning everybody,

After lots of search about this subject, I could find how to get things bound, but I could not find a way to get the value from the component templateUrl.

Anyone knows how to get this value templateUrl from a component inside of $onInit of the associated controller?

Thank you all for your time!


That is my issue, my scope object i am getting empty for the templateUrl:

Component

let newComponent = {
    templateUrl: 'components/new-component/template/field.html',
    controller: TemplateController,
    controllerAs: 'tr',
    bindings: {
        templateUrl: '<'
    }
}

Controller

"use strict";


TemplateController.$inject = ['$scope','$element'];

// Controller Constructor
function TemplateController($scope, $element) {

    // Creating the view model reference
    let vm = this;

    vm.$onInit = function () {
        try {

            console.log($scope.templateUrl);
            debugger;

        } catch (e) {
            console.log(e);
        }
    }

}

export default TemplateController;
1

There are 1 best solutions below

0
Dineshkumar S On

You need to pass template-url attribute for that new component in html file and in that attribute you need to pass that location of that file and in binding you already have templateUrl variable so you can add this in your code to make it working

In html file

<new-component template-url = "components/new-component/template/field.html"></new-component

If you use like this for each component with specific location. It will print location of each file for each component. I think you can print this value in that component controller.