HTML:

<div id="selectedFiles"></div>

Pengontrol:

var selDiv = document.getElementById("selectedFiles");


selDiv.innerHTML +=                         
                    "<span style='margin-right: 5px;'>" +
                    "<button id='deleteButton' ng-
                    click='deleteFile()' " +
                    "style='width: 70px;height: 25px;margin-left: 
                    10px;padding: 1px;'>Delete</button>" +
                    "</span><br/><br/>";

// this was added later but still no luck
$compile($(selDiv).contents())($scope);

function deleteFile() {
        // not getting fired
        console.log("inside delete file1");
    }
0
My God 5 Januari 2018, 12:47

1 menjawab

Jawaban Terbaik

Inilah cara Anda harus melakukannya, IMO:

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

app.controller('ctrl', function($scope) {
  $scope.files = [];

  $scope.deleteFile = function(file) {
    var index = $scope.files.indexOf(file);
    
    if (index > -1) {
      $scope.files.splice(index, 1);
    }
  }
});

angular.module("myApp").directive("filesInput", function() {
  return {
    require: "ngModel",
    link: function postLink(scope,elem,attrs,ngModel) {
      elem.on("change", function(e) {
        var files = [];
        
        for (var i = 0; i < elem[0].files.length; i++) {
          files.push(elem[0].files[i])
        }
        
        ngModel.$setViewValue(files);
      })
    }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<div ng-app="myApp" ng-controller="ctrl">
  <input type="file" files-input ng-model="files" multiple />
  <div ng-repeat="file in files">
    <div>{{ file.name }} <a ng-click="deleteFile(file)" href="">Delete</a></div>
    <br/>
  </div>
</div>

Dapatkan arahan dari di sini.

0
Serge K. 5 Januari 2018, 10:28