【问题标题】:ng-options is not compiled by $compile in directiveng-options 不是由 $compile in 指令编译的
【发布时间】:2015-07-11 03:21:24
【问题描述】:

我尝试了一个没有在指令中使用模板选项的指令,我有一个字段是一个 html 字符串,我正在尝试使用 $compile in 指令进行编译,但它不起作用。不确定我在哪里出错了。

这是我的小提琴:http://jsfiddle.net/egyxjo42/1/

指令名称:my-dropdown-b

【问题讨论】:

  • 请务必在问题本身中包含您的 jsfiddle 代码!

标签: angularjs angularjs-directive


【解决方案1】:

这是因为当您在没有模板的情况下工作时,来自已定义元素的 ng-model 不会被嵌入到新元素中,因此您必须自己添加 ng-model

link: function (scope, element, attrs, ctrl) {
        var ele = "<select ng-options='i.id as i.name for i in foodB' ng-model='"+attrs.ngModel+"'></select>";

        element.html(ele);
        $compile(element.contents())(scope);
    }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-11
  • 2015-10-10
  • 1970-01-01
相关资源
最近更新 更多