首页 > 解决方案 > 将 ngModel 指令添加到 Javascript 中的动态元素

问题描述

有没有办法使用 javascript 将 ng-model 指令应用于创建的元素?在下面的代码中,我希望使用 ng-model 将新的选择元素绑定到控制器内的作用域变量:

angular.module('myApp').directive('dynamicHtml', function ($q, $http, $compile) {
    return {
        restrict: 'EAC',
        scope: '=',
        compile: function(element, attr) {
              return function(scope, element, attr) {

              var select = document.createElement('select');
              // Here I want to use javascript to apply ng-model='controllerVar' 
              // to the new select element
              element.append(select);
             }
    }
};
});

标签: angularjsangularjs-directiveangularjs-controller

解决方案


哎呀-我很抱歉...我忘了编译东西...继续。


推荐阅读