從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