2016-04-05 6 views
0

Wenn ich versuchte, eine Direktive in ngRepeat aufzurufen, schlägt fehl. Aber wenn die gleiche Anweisung außerhalb ngRepeat aufrufen funktioniert gut.Wie kann ich eine Richtlinie in ngRepeat aufrufen?

Bitte überprüfen Sie den Code unten:

<html> 
    <head> 
     <script src ="//ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> 
    </head> 
    <body ng-app = "myApp"> 
     <div ng-controller = "myCtrl"> 
     <div ng-repeat="list in lists"> 
      <div>{{fileName}}</div> 
      <input type = "file" id=list file-model = "myFile"/> 
      <button ng-click = "uploadFile()">upload me</button> 
     </div> 
     </div> 

    <script> 
    var myApp = angular.module('myApp', []); 

    myApp.directive('fileModel', ['$parse', function ($parse) { 
     return { 

      restrict: 'E', 
      link: function($scope, element, attrs) { 
       var model = $parse(attrs.fileModel); 
       var modelSetter = model.assign; 

       element.bind('change', function(){ 
       $scope.$apply(function(){ 
        modelSetter($scope, element[0].files[0]); 
       }); 
       }); 
      } 
     }; 
    }]); 

    myApp.controller('myCtrl', ['$scope', function($scope){ 
     $scope.lists=[1,2,3]; 
     $scope.uploadFile = function(){ 
      var file = $scope.myFile; 

      console.log('file is '); 
      console.dir(file); 
      $scope.fileName=file.name; 

     /* var uploadUrl = "/fileUpload"; 
      fileUpload.uploadFileToUrl(file, uploadUrl);*/ 
     }; 
    }]); 

    </script> 
</body> 
</html> 

Wie die Richtlinie von ng-repeat aufzurufen?

+0

Sie beschränken Ihre Anweisung auf 'E'-Element und verwenden es als Attribut? –

Antwort

0

Sie beschränken Ihre Anweisung auf 'E'-Element und verwenden es als Attribut. Ändern Sie die Eigenschaft restrict auf EA.

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

    myApp.directive('fileModel', ['$parse', function ($parse) { 
     return { 

      restrict: 'EA', 
      link: function($scope, element, attrs) { 
       var model = $parse(attrs.fileModel); 
       var modelSetter = model.assign; 

       element.bind('change', function(){ 
       $scope.$apply(function(){ 
        modelSetter($scope, element[0].files[0]); 
       }); 
       }); 
      } 
     }; 
    }]); 
+0

Hallo, immer noch funktioniert es nicht :-( – Sriraj

Verwandte Themen