我想用AngularJs上傳文件(所以圖片)...我讀AngularJs不支持tag input type =「file」,所以我讀了我需要的自定義指令...我想取輸入文件,並將其用於顯示預覽(縮略圖)在同一個頁面的html和控制器上傳的文件在服務器上...在我的網頁HTML我寫了這個代碼:用AngularJs上傳文件/圖片
<body ng-controller="myController">
<h1>Select file</h1>
<input type="file" file-model="myFile" />
<div>
<h2>File content is:</h2>
<pre>{{ content }}</pre>
</div>
</body>
我寫了這個指令:
var modulo = angular.module('myApp', []);
modulo.directive('fileModel', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.on('change', function (event) {
var file = event.target.files[0];
scope.$emit("fileSelected", file);
});
}
};
});
這是我的控制器:
modulo.controller('myController', function ($scope) {
$scope.$on('fileSelected', function (event, args) {
$scope.content(args.file);
console.log(args.file);
});
});
此代碼不起作用...是否有解決方案?錯誤在哪裏?
來自您發佈的代碼,它似乎缺少一個ng-app聲明 - 將ng-app放在body標籤上,如下所示:
– oliveromahonyI在這裏省略充足,因爲它服務 – Ragnarr