看到这个问题问了几次,但我看不出答案如何应用于我的问题。我在尝试上传文件时不断收到此错误。
Error: Non-assignable model expression: undefined (directive: fileReader)
我该如何解决?
我已经包含了所有相互交互的文件,即使我相当确定 fileDirective.js 中会发生错误
文件指令.js。包含此指令:
spreadsheetApp.directive('fileReader', function() {
return {
scope: {
file: '='
},
restrict: 'E',
template: "<input type='file' onchange='angular.element(this).scope().upload(this)'>",
link: function (scope, element, attrs) {
scope.upload = function (element) {
scope.$apply(function (scope) {
scope.file = element.files[0];
});
};
scope.$watch('file', function () {
if (scope.file) {
var reader = new FileReader();
reader.onload = (function (file) {
return function (env) {
scope.$apply(function () {
scope.file.contents = env.target.result;
});
}
}(scope.file));
reader.readAsText(scope.file);
}
}, true);
}
};
});
(这实际上是从这里借来的。)
索引.html:
<html ng-app="spreadsheetApp">
...
<ng-view></ng-view>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="js/app.js"></script>
<script src="js/directives/fileDirective.js"></script>
<script src="js/controllers/spreadsheetController.js"></script>
...
</html>
app.js: '使用严格';
var spreadsheetApp = angular.module('spreadsheetApp',[]);
spreadsheetApp.config(function($routeProvider) {
$routeProvider.
when('/', {
controller: 'spreadsheetController',
templateUrl: 'views/innerHTML.html'
});
});
innerHTML.html的内容很简单:
<file-reader> </file-reader>