我正在尝试使用基于范围值的differtent模板制作指令.
这是我到目前为止所做的,我不知道为什么不起作用http://jsbin.com/mibeyotu/1/edit
HTML元素:
<data-type content-attr="test1"></data-type>
Run Code Online (Sandbox Code Playgroud)
指示:
var app = angular.module('myApp', []);
app.directive('dataType', function ($compile) {
var testTemplate1 = '<h1>Test1</h1>';
var testTemplate2 = '<h1>Test2</h1>';
var testTemplate3 = '<h1>Test3</h1>';
var getTemplate = function(contentType){
var template = '';
switch(contentType){
case 'test1':
template = testTemplate1;
break;
case 'test2':
template = testTemplate2;
break;
case 'test3':
template = testTemplate3;
break;
}
return template;
};
var linker = function(scope, element, attrs){
element.html(getTemplate(scope.content)).show();
$compile(element.contents())(scope);
};
return {
restrict: "E",
replace: true,
link: linker,
scope: {
content:'='
} …Run Code Online (Sandbox Code Playgroud) 每当我必须在页面中动态包含一个模板时,我一直在使用角度1中的ng-include.
现在如何在角度2中实现这一点.我试过搜索并找到了这些:
https://groups.google.com/forum/#!topic/angular/ROkKDHboWoA,
https://github.com/angular/angular/issues/2753
有人可以解释如何在angular2中执行此操作,因为链接说由于某些安全原因不包括ng-include.
或至少如何在templateUrl属性中使用可验证的值,以便可以在服务器端处理可验证值以提供模板...