How to conditionally apply a template via custom Angular directives?

后端 未结 6 822
攒了一身酷
攒了一身酷 2020-12-29 02:37

DEMO

Consider the following directive:

angular.module(\'MyApp\').directive(\'maybeLink\', function() {
  return {
    replace: true,
    s         


        
6条回答
  •  臣服心动
    2020-12-29 02:48

    I think this is the cleanest way to inject a dynamic template based on a scope property

    angular.module('app')
    .directive('dynamic-template', function () {
      return {
        template:'',
        restrict: 'E',
        link: function postLink(scope) {
          scope.template = 'views/dynamic-'+scope.type+'.html';
        }
      };
    })
    

提交回复
热议问题