genkio / blog

Stay hungry stay foolish
https://slashbit.github.io/blog/
0 stars 1 forks source link

Loading template dynamically in custom directive #103

Open genkio opened 7 years ago

genkio commented 7 years ago
'use strict';

app.directive('chatMessage', function() {
  return {
    restrict: 'E',
    template: '<ng-include src="getTemplateUrl()"/>',
    scope: {
      content: '='
    },
    replace: false,
    controller: Ctrl,
    link: linker
  };

  function Ctrl($scope) {
    $scope.getTemplateUrl = function() {
      return 'assets/views/' + $scope.content.type + '.html';
    };
  }

  function linker(scope, element, attrs) {
    element.on('click', function() {});
    scope.$watchCollection('content', function(newValue, oldValue) {});
  }

});