我要做的是在运行时加载指令然后注册使用。我在plunker中试验让剥离版本正常工作,但我似乎无法这样做。为了在运行时编译指令,您需要引用$compileProvider
,然后在其上声明您的指令。我在一个元素中存储一个字符串,然后在单击一个按钮时评估其内容。该指令只执行alert('hello')
。
基本上我在配置中获取$compileProvider
引用,然后eval()一个声明指令的字符串。
var provider;
angular.module('myApp',[]).config(function(['$compileProvider',function($compileProvider){
provider = $compileProvider;
}]));
//will be loaded from server at runtime
//this is a whole string
var directive = '
provider.directive('log',function(){
return {
link : function()
{
alert('hello');
}
}
});
';
eval(directive);