2017-09-27 85 views
0

我正在嘗試構建一個系統,您可以上傳照片,將照片添加到數組,保存照片。當您將照片保存到API時,也會在上次訪問時使用保存在API中的當前圖像填充陣列。角度CRUD往返於API

所以

  1. 用戶上傳的照片,看到一個預覽(工作)
  2. 單擊添加,它增加了預覽圖像陣列(不完全性工作)
  3. 保存更新陣列和張貼到API 。 (工作,但劑量不會更新陣列異步)

在綜述

我需要從一個API拉,圖像可以被刪除,然後添加回發到API圖像的列表。

我有工作

你上傳圖片,然後單擊添加然後保存並刷新和圖像剛剛發佈的API打印出來。

但是,當我添加圖像,我看不到,直到我保存並刷新。

我需要圖像與圖像列表一起顯示,但不會發布,直到我點擊保存,我還需要一種方法來刪除個別圖像。

程序和API圖片列表的

見截圖打印出來

enter image description here

HTML

<div ng-repeat="campaign in campaigns" class="campaign-container"> 

    <div class="dynamic-upload-container"> 
     <div class="preview"><img style="height: 100px; width: 100px" ng-src="{{preview}}" alt="preview image"></div> 
     <div class="upload-new"> 
      <input id="fileinput" ng-model="file" type="file" ng-model-instant="" name="file" accept="image/*" onchange="angular.element(this).scope().uploadImage(this)"> 

     </div> 
     <div class="slots-container"> 
      <table> 
       <tr> 
        <th><p>Campaign Name</p></th> 
        <th> <strong>{{campaign.c_name}}</strong></th> 
       </tr> 
       <tr> 
        <td><p>Max images allowed</p></td> 
        <td><strong>{{campaign.max_slots}}</strong></td> 
       </tr> 
       </tr> 
      </table> 

      <button ng-click="addImage()">Add image</button> 

      <!--<h3>these are the images pulled from API</h3>--> 
      <!--<strong>{{campaign.slots}}</strong>--> 

      <h3>these are the images added from preview</h3> 
       <div ng-repeat="slot in campaign.slots" class="slot"> 
        <img ng-click="addImage()" style="height: 100px; width: 100px" ng-src="{{slot.base_image}}" alt="slot image"> 

        <!--<strong ng-click="addImage()" style="height: 100px; width: 100px">{{campaign.slots}}}</strong>--> 

        <button ng-click="removeImage(slot)">Remove Image</button> 
      </div> 

      <button ng-click="SaveImage()">Save to API</button> 
     </div> 
    </div> 

的JavaScript

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

    $scope.campaigns =[]; 
    $scope.preview = ''; 
    $scope.slots = []; 
    $scope.maxSlots = 5; // this dynamic 

    $scope.safeApply = function(fn) { 
     if (this.$root) { 
      var phase = this.$root.$$phase; 
      if (phase == '$apply' || phase == '$digest') { 
       if (fn && (typeof(fn) === 'function')) { 
        fn(); 
       } 
      } else { 
       this.$apply(fn); 
      } 
     } else { 
      fn(); 
     } 
    }; 

    $scope.debug = function(){ 
     console.log('this is debug'); 
     console.log($scope.slots.length); 
    }; 

    $scope.uploadImage = function() { 
     // console.log('we are here'); 
     input = document.getElementById('fileinput'); 
     file = input.files[0]; 
     size = file.size; 
     if (size < 650000) { 
      var fr = new FileReader; 
      fr.onload = function (e) { 
       var img = new Image; 

       img.onload = function() { 
        var width = img.width; 
        var height = img.height; 
        if (width == 1920 && height == 1080) { 
         $scope.preview = e.target.result; 
         $scope.perfect = "you added a image"; 
         $scope.$apply(); 

        } else { 
         $scope.notPerfect = "incorrect definitions"; 
         $scope.$apply(); 
        } 
       }; 
       img.src = fr.result; 
      }; 

      fr.readAsDataURL(file); 
     } else { 
      $scope.notPerfect = "to big"; 
     } 
    }; 

    $scope.addImage = function() { 
     if ($scope.slots.length < $scope.maxSlots) { 
      $scope.slots.push({ 
       "slot_id": $scope.slots.length + 1, 
       "base_image": $scope.preview, 
       "path_image": "" 
      }); 
      $scope.safeApply 

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

    $scope.SaveImage = function() { 
     $http({ 
      url: "http://www.site.co.uk/ccuploader/campaigns/updateSlots", 
      method: "POST", 
      data: { 'campaign': "ben", 'slots': $scope.slots }, 
      headers: {'Content-Type': 'application/json'} 
     }).then(function (response) { 
      // success 
      console.log('success'); 
      console.log("then : " + JSON.stringify(response)); 
      // location.href = '/cms/index.html'; 
     }, function (response) { // optional 
      // failed 
      console.log('failed'); 
      console.log(JSON.stringify(response)); 
     }); 
    }; 

    $scope.removeImage = function(s) { 
     $scope.campaign.slots.splice($scope.campaign.slots.indexOf(s), 1); 
    }; 

    $scope.GetData = function() { 
     $http({ 
      url: "http://www.site.co.uk/ccuploader/campaigns/getCampaign", 
      method: "POST", 
      date: {}, 
      headers: {'Content-Type': 'application/json'} 
     }).then(function (response) { 
      // success 
      console.log('you have received the data '); 
      console.log(response); 

      $scope.campaigns = response.data; 
      //$scope.slots = data.data[0].slots; 

     }, function (response) { 
      // failed 
      console.log('failed getting campaigns goo back to log in page.'); 
      console.log(response); 
     }); 
    }; 

    $scope.GetData(); 
}) 
+0

究竟是什麼問題?無法保存新圖像?你有一系列圖像,而不是添加或刪除圖像更新? –

+0

因此,目前我可以從API打印出我的圖像,我也可以通過單擊添加圖像將圖像添加到列表中,然後刪除該圖像,然後在刷新時保存以添加到API,我會看到圖像的base64我剛剛保存的圖像。我想從API的base64圖像數組中填充列表以及刪除和添加的能力 – Beep

+0

Ill更新我的問題以使其更容易閱讀 – Beep

回答

0

已解決通過更改GetData函數路徑從

$scope.GetData = function() { 
     $http({ 
      url: "http://www.ccuktech.co.uk/ccuploader/campaigns/getCampaign", 
      method: "POST", 
      date: {}, 
      headers: {'Content-Type': 'application/json'} 
     }).then(function (response) { 
      // success 
      console.log('you have received the data '); 
      // console.log(response); 
      $scope.campaigns = response.data; 
      $scope.slots = response.data[0].slots; 

     }, function (response) { 
      // failed 
      console.log('failed getting campaigns goo back to log in page.'); 
      // console.log(response); 
     }); 
    };