0

I am trying to implement directive for dynamic template. Depending on what is selected in drop-down list, template should change. I only manage so far to implement to be dynamic on page load, but when value in drop-down list is changed, template stays the same..`(function () { 'use strict';

// directive for loading incomeinformation, regular or military
angular.module('myModule').directive('dynamicTemplate', templateControl);

templateControl.$inject = [];

function templateControl() {
    var directive = {
        controller: mycontroller,
        controllerAs: 'mycontrollerCtrl',
        bindToController: true,
        restrict: 'E',
        template: '<ng-include src="mycontrollerCtrl.getTemplateUrl()"/>'
    };

    return directive;

    function mycontroller($attrs, $element, $scope, $compile) {
        var vm = this;
        vm.getTemplateUrl = getTemplateUrl;           
        function getTemplateUrl() {
            if ($attrs.templateId == 1)
                return "test1.html";
            if ($attrs.templateId == 3)
                return "test1.htm2";

            return "test3.html";
        }
    }
}

})();`

And in html: <dynamic-template templateid="{{model.TemplateId}}"></imp-dynamic-template>

2
  • I think you might be taking a wrong approach (in angular terms) to solve the problem. Could you please describe business functionality that you are trying to achieve ? Commented Feb 20, 2015 at 11:38
  • I have a product types, and based on which product is selected different template for edit needs to be displayed Commented Feb 20, 2015 at 11:43

1 Answer 1

1

in html: attribute name must be "template-id" because you access it like $attrs.templateId

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

Comments

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.