从json获取最大插槽并应用于控制器

[英]Get max slots from json and apply to controller


I have a upload form where once you upload and preview the image you can add it to a list depending that the list has less than the max slots filled.

我有一个上传表单,一旦您上传和预览图像,您可以将其添加到列表中,具体取决于列表的填充小于最大插槽数。

for example:

例如:

If max slots is 5 then you can add 5 images, if more then the program will tell you to remove a image as you have hit the max slots.

如果最大插槽为5,那么您可以添加5个图像,如果更多,则程序会告诉您在达到最大插槽时删除图像。

my json looks as following

我的json看起来如下

json array

Error I'm Getting

我得到的错误

I am not getting to upload and keep getting

我无法上传并继续上传

TypeError: Cannot read property 'max_slots' of undefined

even though the path in my json to max slots is campaigns[index].max_slots as you can see in the image above.

即使我的json到最大插槽的路径是广告系列[index] .max_slots,如上图所示。

Im not sure whats going wrong can someone help out please ?

我不确定有什么问题会有人帮帮忙吗?

JavaScript

JavaScript的

.controller('Dashboard', function ($scope, $http, $timeout) {

        $scope.campaigns = [];
        $scope.preview = '';


        $scope.addImage = function (index) {

            if ($scope.length < $scope.campaigns[index].max_slots) {
                $scope.slots.push({
                    "slot_id": $scope.length + 1,
                    "base_image": $scope.preview,
                    "path_image": ""
                });

            } else {
                window.alert("you have to delete a slot to generate a new one");
            }
        };

1 个解决方案

#1


0  

$scope.addImage function index variable may be has wrong value and $scope.campaigns[index] is undefined.

$ scope.addImage函数索引变量可能有错误的值,$ scope.campaigns [index]未定义。

if your have ng-repeate loop and ng-click with addImage($index) then on click the function will pass wrong value i.e. $scope.campaigns.length. Please use single campaign object instead of index number.

如果你有ng-repeate循环并使用addImage($ index)进行ng-click,那么单击该函数将传递错误的值,即$ scope.campaigns.length。请使用单个广告系列对象而不是索引号。

The function like

功能就像

$scope.addImage = function (campaign) {
    if(!campaign) return;
    if ($scope.length < campaign.max_slots) {
        $scope.slots.push({
            "slot_id": $scope.length + 1,
            "base_image": $scope.preview,
            "path_image": ""
        });
    } else {
        window.alert("you have to delete a slot to generate a new one");
    }
};

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/09/28/4f52fc500248a357feb7bb9ea5e8bd2b.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com