JavaScript/ AngularJS:只有当我单击两次按钮时才会显示错误消息

我有一个angularJS视图,允许用户上传Excel数据并将其导入SQL服务器表。


我添加了数据的客户端验证,但仅当我再次单击“上载/导入”按钮时,才会显示错误消息。我不确定代码中的哪个部分导致了此问题。


是否可以寻求帮助以尝试解决此问题?我想我可能需要移动变量赋值的位置,但在哪里?$scope.Message


我包括Javascript / angularjs代码和html视图。


AngularJS


//Parse Excel Data   

$scope.ParseExcelDataAndSave = function () {


    var file = $scope.SelectedFileForUpload;


    if (file) {


        var reader = new FileReader();


        reader.onload = function (e) {


            var filename = file.name;

            // pre-process data

            var binary = "";

            var bytes = new Uint8Array(e.target.result);

            var length = bytes.byteLength;


            for (var i = 0; i < length; i++) {

                binary += String.fromCharCode(bytes[i]);

            }


            // call 'xlsx' to read the file

            var data = e.target.result;

            var workbook = XLSX.read(binary, { type: 'binary', cellDates: true, cellStyles: true });

            var sheetName = workbook.SheetNames[0];

            var excelData = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);


            exceljsonObj = excelData;


            var errors = [];

            var rowCounter = 1;


            for (var j = 0; j < exceljsonObj.length; j++) {


                var xdata = exceljsonObj[j];


                if (xdata['Meeting ID'] === undefined || xdata['MeetingID'] === undefined) {


                    errors.push('Row: ' + rowCounter + ' is missing the Meeting ID value.' +

                                ' Please, verify that the data you are trying to upload meets the required criteria, ' +

                                'and then try to upload your file again.');

                }

};


收到一只叮咚
浏览 79回答 1
1回答

胡说叔叔

$scope摘要问题。分配$scope后调用 $scope.apply()。消息if (errors.length > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $scope.Message = 'Please, verify that the file you are trying to upload is correctly formatted, \n ' +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'and that the data it contains, meets the expected criteria, then click the upload button again. \n Thank you!'&nbsp; &nbsp; $scope.$apply();&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript