【问题标题】:Post final json nested array variables发布最终的 json 嵌套数组变量
【发布时间】:2018-03-11 04:37:59
【问题描述】:

我有一个添加函数,我将图像添加到一个名为 slot 的数组中的活动(每个活动都有自己的槽数组)。

添加到列表后,我希望发布到终点。

问题

当我发布时,我得到了错误

TypeError: Cannot read property 'slots' of undefined

我需要一些帮助来修复这篇文章,我看不出错误是什么以及如何修复它。

我正在尝试发布当前的campaignbase_image

"campaign": ,
  "slots": [
    {
      "base_image": 
    }
  ]

HTML

 <div ng-repeat="campaign in campaigns" class="campaign-container">
    <div class="container">
        <h1>{{campaign.c_name}} {{$index}}</h1><strong>This Campaign you are allowed {{campaign.max_slots}}
        Images</strong>
        <table class="table">
            <thead>
            <tr>
                <th>Select File</th>
                <th>Preview Image</th>
                <th>Add to list</th>
                <th>Images</th>
                <th>Save Campaign</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td>
                    <!-- UPLOAD IMAGE-->
                    <div class="upload-new">
                        <input type="file" fileread="vm.uploadme" id="fileinput-{{ $index }}"
                               onchange="angular.element(this).scope().uploadImage(this)"/>
                    </div>
                    <!-- END-->
                </td>
                <td>
                    <!-- PREVIEW IMAGE-->
                    <div class="preview">
                        <img style="height: 100px; width: 100px" ng-src="{{campaign.preview}}" alt="preview image">
                    </div>
                    <!-- END-->
                </td>
                <td>
                    <button ng-click="addImage(campaign)">
                        <i class="fa fa-plus-circle" style="font-size: 45px;" aria-hidden="true"></i>
                    </button>
                </td>
                <td>

                    <div ng-repeat="slot in campaign.slots" class="slot">
                        <img style="height: 100px; width: 100px" ng-src="{{slot.base_image}}" alt="show image here">
                        <button ng-click="removeImage(slot,campaign)">Remove Image</button>

                    </div>
                </td>
                <td>
                    <button ng-click="SaveImage()">
                        <i class="fa fa-floppy-o" style="font-size: 45px;" aria-hidden="true"></i>
                    </button>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
</div>

JavaScript

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

    $scope.SaveImage = function (campaign) {
        // console.log($scope.campaigns[index].c_name);
        $http({
            url: "http://www.site.co.uk/ccuploader/campaigns/updateSlots",
            method: "POST",
            data: { 'slots': campaign.slots},
            headers: {'Content-Type': 'application/json'}
        }).then(function (response) {
            // success
            console.log('success',response);
            window.alert("success, campaign has been saved");
        }, function (response) { // optional
            // failed
            console.log('failed', response);
        });
    };

【问题讨论】:

  • $scope.SaveImage 是如何绑定/调用的?
  • 使其campaign 可通过$scope.campaign 访问
  • @user2486 hmm 不起作用,我已经更新了问题以显示我的 json 的结构
  • @NathanP。我现在更新 html
  • 同一个问题,半小时前我在你上一个问题中解释过。您的 JS 和 HTML 中的函数签名需要匹配。

标签: javascript html angularjs arrays json


【解决方案1】:

将您的绑定更改为:

<button ng-click="SaveImage(campaign)">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2019-02-23
    • 2019-02-07
    • 1970-01-01
    相关资源
    最近更新 更多