【问题标题】:how to customize angular toaster message如何自定义角度烤面包机消息
【发布时间】:2015-07-07 11:49:26
【问题描述】:

我正在使用角度文件上传。我已经设置了一个批处理文件上传,它解析文件名并将它们与存储在数据库中的属性相匹配。文件的结构需要像这样。

01-1998 VRF RD678.pdf

VRF 是管道的名称 RD 是位置的名称 678是位置代码的编号

他们每个人都有自己的 if 语句来检查与数据库中的任何内容匹配的文件。现在,如果某些内容不匹配或名称不正确,则会出现

我想做三件事。

  1. 定义一条错误消息,显示文件名和出错的特定 if 语句。如果管道不匹配,我想要文件名和下面的“管道不匹配”。

  2. 定义文件名结构不正确时的错误消息。我想要下面带有“不正确文件名”的文件名

  3. 防止函数出错,我希望显示错误消息并允许上传其他文件

我正在尝试使用 linq.js,javascript 也可以。

这就是我想要做的工作,这个例子是当文件结构不正确时。这个错误信息是

TypeError: 无法读取未定义的属性“名称”

$scope.upload = function () {
    var files = $scope.files;
    if (files && files.length) {
        for (var i = 0; i < files.length; i++) {
            var file = files[i];
            $scope.pipes.map(function (pip) {
                $scope.pipeLookup[pip['PipeAb']] = pip;
            });
            $scope.locations.map(function (loc) {
                $scope.locationLookup[loc['LocationAb']] = loc;
            });
            $scope.locationCodes.map(function (locCode) {
                $scope.locationCodeLookup[locCode['LocationCodeAb']] = locCode;
            });
            var matchesPip = file.name.match(/^\d+\D\d+\s*(\S*\s*)(\S*)/i);
            var matchesLoc = file.name.match(/^\d+\D\d+\s*?(\S*)\s*(\S*?)(\d+)\./i);
            var matchesLocCode = file.name.match(/^(\d+\D\d+)\s*?(\S*)\s*(\S*?)(\d+)\./i);
            $scope.pip = $scope.pipeLookup[matchesPip[1]];
            $scope.loc = $scope.locationLookup[matchesLoc[2]];
            $scope.locCode = $scope.locationCodeLookup[matchesLocCode[4]];
            if ($scope.pip == null) {

                $scope.pip = Enumerable.From(files)
                            .Where("x => x.files.name != '" + matchesPip[0] + "'").ToArray();

                toaster.pop('error', matchesPip[0]);
                console.log(matchesPip[0])
            }
            if ($scope.loc == null) {
                toaster.pop('error', matchesLoc[0]);
                console.log(matchesLoc[0])
            }
            if ($scope.locCode == null) {
                toaster.pop('error', matchesLocCode[0]);
                console.log(matchesLocCode[0])
            }
            $upload.upload({
                url: '/api/apiBatchPipeLine',
                fields: {
                    'typeId': 1,
                    'companyId': $scope.companyId.CompanyId,
                    'companyName': $scope.companyId.CompanyName,
                    'documentDate': $scope.model.documentDate,
                    'pipeId': $scope.pip['PipeId'],
                    'pipeName': $scope.pip['PipeName'],
                    'locationId': $scope.loc['LocationId'],
                    'locationAb': $scope.loc['LocationAb'],
                    'locationCodeId': $scope.locCode['LocationCodeId'],
                    'locationCodeAb': $scope.locCode['LocationCodeAb']
                },
                file: file
            }).progress(function (evt) {
                var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
            }).success(function (data, status, headers, config) {
                toaster.pop('success', config.file.name);
                console.log('file ' + config.file.name + 'uploaded. Response: ' + data);
            }).error(function (err, result, config) {
                toaster.pop('error', config.file.name);
                console.log(err, result);
            });

        }
    }
};

【问题讨论】:

  • 如果是文件错误出现的时候,那么你应该关注这一行toaster.pop('error', config.file.name);config 对象没有 file 对象。我的猜测是因为它实际上是result.file.name
  • 可能config.file 未在此行设置(未定义)toaster.pop('error', config.file.name);
  • 而不是第三方去自定义全局指令。
  • 你能分享一下我应该使用什么指令
  • 使用matrial angular库。吐司在那里真的很容易

标签: javascript angularjs if-statement toastr


【解决方案1】:

ngFileUpload error 事件回调接收 4 个参数,如下所示:

https://github.com/danialfarid/ng-file-upload/blob/master/dist/ng-file-upload-all.js#L509-514

promise.error = function (fn) {
   promise.then(null, function (response) {
       fn(response.data, response.status, response.headers, config);
   });
   return promise;
};

headers 是第三个参数,config 是第四个参数。在您的代码中,config 引用了headersheaders.file 未定义,所以这就是你得到错误 TypeError: Cannot read property 'name' of undefined 的方式。

变化:

.error(function (err, result, config) {
   ...
})

收件人:

.error(function (err, result, headers, config) {
   ...
})

【讨论】:

    猜你喜欢
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-19
    • 2018-02-26
    • 1970-01-01
    相关资源
    最近更新 更多