2017-08-14 98 views
0

我得到这个custome指令:的OnChange触发的第一个文件输入事件

angular.module('uvox-player').directive('customOnChange', function() { 
    return { 
     restrict: 'A', 
     link: function (scope, element, attrs) { 
      var onChangeHandler = scope.$eval(attrs.customOnChange); 
      element.bind('change', onChangeHandler); 
     } 
    }; 
}); 

和2个不同的输入一个观点:

<div ng-show="platform == 'darwin'"> 
    <input class="ng-hide" id="input-file-id" multiple type="file" custom-on-change="importPlaylist"/> 
    <label for="input-file-id" class="md-button md-raised md-primary">Import PlayList</label> 
</div> 


<div> 
    <input class="ng-hide" id="input-file-id" multiple type="file" custom-on-change="importCover"/> 
    <label for="input-file-id" class="md-button md-raised md-primary">New Cover</label> 
</div> 

当我点击第二个输入触发的事件是一个在第一个输入中声明:importPlaylist。

我该如何解决这个问题? 谢谢!

额外:的方法,其中的onChange点:

angular.module('uvox-player').controller('PlaylistBaseController', function($scope, Api, $state, $q, $http, $mdToast, ngDialog, $mdDialog) { 

$scope.importPlaylist = function(event) { 
    var file = event.target.files[0]; 
    Api.postPlaylistItunes('itunes[file]', file).then(function(data) { 
     $state.reload(); 
    }, function(error) { 
    }); 

} 

和第二输入方法:

angular.module('uvox-player').controller('PlaylistCoverController', function($scope, Api, $state, $q, $http, $mdToast, ngDialog, $stateParams) { 

$scope.selectPlaylist($stateParams.id); 
Api.getPlaylist($stateParams.id).then(function(data) { 
    $scope.cover = data.cover; 
}, function(error) { 
}); 


$scope.importCover = function(event) { 
    var file = event.target.files[0]; 
    Api.postPlaylistCover($scope.selectedPlaylist, 'cover[file]', file).then(function(data) { 
     $state.reload(); 
    }, function(error) { 
    }); 

} 

});

+1

为了调试的目的,您是否可以删除第一个输入并查看该行为是否持续? – Vega

+0

是的,已经尝试过,它工作正常。 –

+0

你是否已经修好了发行? – Vega

回答

0

虽然右控制器进行路由上定义,故障持续,直到我意识到,不稳定的行为是具有两个输入上的相同的id,作为复制/粘贴的结果的结果.... 更改了ID,问题解决了....

0

如果你没有声明控制器AngularJs会在主控制器中查找,并且在你的情况下它可以在PlaylistBaseController中找到方法importCover。

<div ng-controller="PlaylistBaseController" ng-show="platform == 'darwin'"> 
    <input class="ng-hide" id="input-file-id" multiple type="file" custom-on-change="importPlaylist"/> 
    <label for="input-file-id" class="md-button md-raised md-primary">Import PlayList</label> 
</div> 


<div ng-controller="PlaylistCoverController"> 
    <input class="ng-hide" id="input-file-id" multiple type="file" custom-on-change="importCover"/> 
    <label for="input-file-id" class="md-button md-raised md-primary">New Cover</label> 
</div> 
+0

非常有意义;-)我会在今天晚上检查并确认并投票。谢谢soooo多;-) –

+0

不客气:) – Vega

+0

刚刚检查过...没有运气:-(它仍然调用第一个方法 –