gpt4 book ai didi

AngularJS 错误 : Non-assignable model expression

转载 作者:行者123 更新时间:2023-12-03 17:52:27 25 4
gpt4 key购买 nike

看到这个问题问了几次,但我看不出答案如何应用于我的问题。我在尝试上传文件时不断收到此错误。
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);
}
};
});

(这实际上是从 here 借来的。)

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>

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>

最佳答案

在我看来,您似乎还没有定义 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) {
...

或者更好:
 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) {
...

JSFiddle的工作文件上传。

关于AngularJS 错误 : Non-assignable model expression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17686524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com