$(this).append()在angularjs中不起作用

时间:2018-01-02 04:42:30

标签: jquery angularjs

当我尝试追加angularjs时,我收到了类似

的错误
  

jquery.min.js:3未捕获的TypeError:无法读取属性

     

' createDocumentFragment'未定义的

我收到的错误是

  $(this).append(stringDir);

其中stringDirp元素。我将context:this添加到了我的ajax调用中   修复错误,但仍显示错误。

    var app = angular.module('fileApp', []);

    app.controller('fileController', function($scope,$http) {
    $scope.getFile = function(event) {
        $scope.status = $scope.clickStatus;
        if($scope.status=="0" ) {   
            $scope.clickStatus = "1";
            $scope.arrowStatus = "1";

            $scope.data = {
               path : $scope.current_directory
            };  

            $(this).find($(".arrow ")).removeClass('fa fa-caret-right').addClass('fa fa-caret-down');

            $.ajax({
                type: "post",
                url: "/cfc/file.cfc?method=show",
                dataType : 'JSON',
                data : { path : $(this).attr('current_directory') },    
                context :this,  
                success:function(response) {
                var i=0;
                    if(response) {
                        var stringFile='';
                        var stringDir = '';
                        for(i=0; i< response.DATA.length;i++){
                            var val=(response.DATA[i]);
                            if(val[2]==="Dir"){
                                var newPath = val[6]+"\\"+val[0];
                                console.log(newPath);
                                stringDir += '<p class="dirClass" style="margin-left:15px" current_directory="'+newPath+'"  ><span ><i class="fa fa-caret-right arrow"></i></span><span> <i class="fa fa-folder-open folder_icon" aria-hidden="true"></i></span>'  + val[0]+'</p>';
                            }
                            else{
                                var newPath = val[6]+"\\"+val[0];
                                stringFile += '<p class="fileClass " style="margin-left:18px" current_directory="'+newPath+'"><i class="fa fa-file file_icon" aria-hidden="true"></i>  '+ val[0] +'</p>';
                            }
                        }  
                        $(this).append($(stringDir)); 
                        $(this).append($(stringFile));   
                    }
                },
                error:function (data){
                console.log(data);
                }
            });
        }
    }

   });

 HTML

<body ng-app="fileApp" ng-controller="fileController" >
<div class="row left_row_content_drive" id ="left_row_content_drive">
<div class="localdisc_d_click" id="container_id" >
<p class="dirClass" ng-init = "current_directory= 'D:\projects' 
;clickStatus = '0' ;arrowStatus= '0' " ><i class="fa fa-hdd-o 
drive_icon"  aria-hidden="true" ng-click="getFile()"></i> Local Disc D 
</p>
</div>
</div>
</body>

2 个答案:

答案 0 :(得分:-1)

不要在ajax成功回调块中使用它。而是在ajax调用之前为元素创建一个变量,然后引用该变量来访问成功回调中的该元素。像下面的东西

 var element = this; //Hold the element in 'element' variable


   //Your ajax call
    $.ajax({
       url: "Any Url",
       success: function(response){
     if (response) {
      var stringFile = '';
      var stringDir = '';
      for (i = 0; i < response.DATA.length; i++) {
        var val = (response.DATA[i]);
        if (val[2] === "Dir") {
          var newPath = val[6] + "\\" + val[0];
          console.log(newPath);
          stringDir += '<p class="dirClass" style="margin-left:15px" 
          current_directory = "'+newPath+'" > < span > < i class =
            "fa fa-caret-right arrow" >
            <
            /i></span > < span > < i class =
            "fa fa-folder-open folder_icon"
          aria -
            hidden = "true" > < /i></span > '  + val[0]+' < /p>';
        } else {
          var newPath = val[6] + "\\" + val[0];
          stringFile += '<p class="fileClass " style="margin-left:18px" 
          current_directory = "'+newPath+'" > < i class =
            "fa fa-file file_icon"
          aria -
            hidden = "true" > < /i>  '+ val[0] +'</p > ';
        }
      }
      $(element).append(stringDir);
      $(element).append(stringFile);
    }
       }

    })

答案 1 :(得分:-1)

你就在这里&#39;这个&#39;是控制器,对不起!我的错误,但如果你更换了这个&#39;使用元素引用,此代码将运行