无法动态创建指令

时间:2016-02-12 18:57:31

标签: javascript angularjs angularjs-directive

我要做的是在运行时加载指令然后注册使用。我在plunker中试验让剥离版本正常工作,但我似乎无法这样做。为了在运行时编译指令,您需要引用$compileProvider,然后在其上声明您的指令。我在一个元素中存储一个字符串,然后在单击一个按钮时评估其内容。该指令只执行alert('hello')

Plunker for clarity

基本上我在配置中获取$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);

0 个答案:

没有答案