javascript - $compile 未定义

标签 javascript html angularjs dom

使用 angular.elements 动态创建元素 - AngularJS

我需要使用 Angular 创建一个指令元素。但使用 angular.element 没有运气。最终出现 $compile is not Defined 错误。非常感谢任何帮助。

文本字段-personal.html

<input ng-focus="setIndex(field.id);field.focus()" id="{{field.id}}"
       type="{{field.type}}" name="{{field.id}}" ng-model="field.text"
       class="login_input_personal {{field.selectedClass}} {{enabledCursor}} {{field.text == '' ? 'blank' : ''}}"
       ng-mouseup="clearSelected(field.id);"
       ng-mousedown="startDrag(field.id);" />
<div class="placeholder" ng-show="field.text == ''"
  {{interfaceLabels[field.defaultText]}}
</div>
<div class="placeholder" ng-show="field.text == ''"></div>
<a class="clear-textfield" ng-click="clearTextField();"
   ng-if="emailTextfieldClearButton"
   ng-show="field.text != '' && field.selectedClass != ''">
</a>

这是我的

textField.js

angular
.module("textBoxUi")
.directive("textFieldPersonal", ['$rootScope', function($rootScope) {
    return {
        restrict : 'E',
        templateUrl : 'components/text-field-personal.html',
        scope : {
         field : "="
        },

        link : function (scope, element, attr) {
            scope.interfaceLabels = scope.$parent.interfaceLabels;
            scope.emailTextfieldClearButton = $rootScope.emailTextfieldClearButton;
            scope.enabledCursor = $rootScope.enabledCursor;
            scope.setIndex= function(id){
                $rootScope.textFieldIndex = document.getElementById(id).selectionStart;
            };
            scope.startDrag = function(e){
                var e = window.event;
                scope.startDragX = e.pageX;
                e = null;
            };
            scope.clearSelected = function(id){
                var e = window.event;
                scope.stopDragX = e.pageX;
                if(scope.stopDragX > scope.startDragX){
                    document.getElementById(id).setSelectionRange(document.getElementById(id).selectionEnd, document.getElementById(id).selectionEnd);
                    $rootScope.textFieldIndex = document.getElementById(id).selectionEnd;
                }else {
                    document.getElementById(id).setSelectionRange(document.getElementById(id).selectionStart, document.getElementById(id).selectionStart);
                    $rootScope.textFieldIndex = document.getElementById(id).selectionStart;
                }
                e = null;
            };
            scope.clearTextField = function(){
                $rootScope.targetField.text ="";
                $rootScope.hideEmailExtensionTop();
                document.getElementById($rootScope.targetField.id).value = $rootScope.targetField.text;
                if(typeof(scope.field.callback) === "function"){
                    scope.field.callback();
                }
            };

            scope.enabledCursorWatch = $rootScope.$watch('enabledCursor', function(){
                scope.enabledCursor = $rootScope.enabledCursor;
            });

        }
    }
}]);

在我的index.html中,我将使用如下指令

<text-field-personal> </text-field-personal>

但我想在 javascript DOM 元素中创建相同的内容。尝试了如下但没有运气。

  var divv = document.createElement("div");
  var newDirective = angular.element("<input ng-focus='setIndex(field.id);field.focus()' id='{{field.id}}' type='{{field.type}}' name='{{field.id}}' ng-model='field.text' class='login_input_new {{field.selectedClass}} {{enabledCursor}} {{field.text == '' ? 'blank' : ''}}' ng-mouseup='clearSelected(field.id);' ng-mousedown='startDrag(field.id);'/><a class='clear-textfield' ng-click='clearTextField();' ng-if='emailTextfieldClearButton' ng-show='field.text != '' && field.selectedClass != '''></a>");
  divv.append(newDirective);
  $compile(newDirective)($scope);

最佳答案

$compile 未定义,因为它未注入(inject)指令中:

angular
.module("textBoxUi")
̶.̶d̶i̶r̶e̶c̶t̶i̶v̶e̶(̶"̶t̶e̶x̶t̶F̶i̶e̶l̶d̶P̶e̶r̶s̶o̶n̶a̶l̶"̶,̶ ̶[̶'̶$̶r̶o̶o̶t̶S̶c̶o̶p̶e̶'̶,̶ ̶f̶u̶n̶c̶t̶i̶o̶n̶(̶$̶r̶o̶o̶t̶S̶c̶o̶p̶e̶)̶ ̶{̶ 
.directive("textFieldPersonal", function($rootScope, $compile) {

关于javascript - $compile 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53467026/

相关文章:

javascript - 带有集群和过滤器复选框的 Mapbox 个性化图标

javascript - 我可以更改参数还是操作是静态的/"locked down"吗?

javascript - 将 html 打印到多个页面

html - 表格中的字体颜色不继承自父 div

javascript - 限制 AngularJS 中的分页

javascript - 使用 Jest 测试时使用 webpack 导入图像时 enzyme 渲染失败

javascript - 将 Angular 表单提交到 php 文件

html - 自定义复选框适用于 Chrome 和 Safari 但不适用于 Firefox 和 IE

javascript - promise 不适用于 angularjs 中的 IndexedDB

javascript - 递归搜索 JSON 字符串字典,查找键值对在 "name"中的所有值