看到这个问题问了几次,但我看不出答案如何适用于我的问题。尝试上传文件时,我一直收到此错误。
Error: Non-assignable model expression: undefined (directive: fileReader)
我该如何解决?
我已经包含了所有相互交互的文件,即使我可以肯定在fileDirective.js中发生了错误
fileDirective.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);
}
};
});
Run Code Online (Sandbox Code Playgroud)
(这实际上是从这里借来的。)
index.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>
Run Code Online (Sandbox Code Playgroud)
app.js: '使用严格';
var spreadsheetApp = angular.module('spreadsheetApp',[]);
spreadsheetApp.config(function($routeProvider) {
$routeProvider.
when('/', {
controller: 'spreadsheetController',
templateUrl: 'views/innerHTML.html'
});
});
Run Code Online (Sandbox Code Playgroud)
而且innerHTML.html的内容很简单:
<file-reader> </file-reader>
对我来说,您似乎没有定义该fileReader指令属于正确的模块。
尝试添加此顶行:
var spreadsheetApp = angular.module('spreadsheetApp');
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) {
...
Run Code Online (Sandbox Code Playgroud)
或者更好的是:
angular.module('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) {
...
Run Code Online (Sandbox Code Playgroud)
JSFiddle的一个工作文件上传。
| 归档时间: |
|
| 查看次数: |
7032 次 |
| 最近记录: |