【问题标题】:After refresh the page image is not show in the view in AngularJS?刷新后页面图像不显示在 AngularJS 的视图中?
【发布时间】:2018-01-28 10:30:22
【问题描述】:

我必须使用 Firebase 在 AnguarJS 中上传图像,但问题是图像已正确上传,并且我必须将 imageURL 存储到 localStorage 但刷新页面图像后我不明白什么问题

$scope.backgroundImageURL  = [];
$scope.currentUserObject   = {};
$scope.uploadBackgroundImage = function(event) {
//Get the userDetail of the current logged in user
$scope.currentUserObject =  localStorage.getItem('userName');

   //Get the value from the input field and assign into the fireabse node
   var userProductImg       = $("#getImageAttribute")[0].files[0];
   var PRODUCT_STORAGE_REF  = firebase.storage().ref('user/image');

   //get the date as well as put the imageURL from node
  var rn = new Date().getTime().toString();
  var task =              PRODUCT_STORAGE_REF.child("loggedInUserObject").child($scope.currentUserObject).child(rn).put(userProductImg).then(function(snapshot) {
      
      $timeout(function(){
           $scope.backgroundImageURL.push(snapshot.downloadURL);
          localStorage.setItem('userImageURL', $scope.backgroundImageURL);
       }, 50);
  })
 }
<input type="file" id="getImageAttribute" ng-click="$event = $event" ng-      model="display" multiple                  onchange="angular.element(this).scope().uploadBackgroundImage(event)"
/>
<span ng-repeat="imgURL in backgroundImageURL"> 
<img src="{{imgURL}}">
</span>

?

【问题讨论】:

    标签: javascript angularjs firebase


    【解决方案1】:

    尝试使用ng-src 而不是src 属性,因为如果免费的img url 发生动态变化,它将刷新图像。

    您可以从这里获得更多信息:https://www.w3schools.com/angular/ng_ng-src.asp 或者 你也可以试试这个:https://docs.angularjs.org/api/ng/directive/ngSrc

    【讨论】:

    • 它对我不起作用,先生,刷新页面图像后使用 ng-src 后再次删除?
    【解决方案2】:

    刷新页面后,你的图片数组为空:

    $scope.backgroundImageURL = [];

    您需要一种方法从存储中获取图像并将它们分配给您的 backgroundImageURL 数组,并且必须在加载页面后立即调用此方法。

    您的代码可能类似于以下内容:

    $scope.backgroundImageURL  = [];
    $scope.currentUserObject   = {};
    
    $scope.loadResources();
    
    $scope.loadResources = function() {
      $scope.backgroundImageURL = localStorage.getItem('userImageURL');
      $scope.currentUserObject = localStorage.getItem('userName');
    }
    
    $scope.uploadBackgroundImage = function(event) {
    //Get the userDetail of the current logged in user
    $scope.currentUserObject =  localStorage.getItem('userName');
    
       //Get the value from the input field and assign into the fireabse node
       var userProductImg       = $("#getImageAttribute")[0].files[0];
       var PRODUCT_STORAGE_REF  = firebase.storage().ref('user/image');
    
       //get the date as well as put the imageURL from node
      var rn = new Date().getTime().toString();
      var task =              PRODUCT_STORAGE_REF.child("loggedInUserObject").child($scope.currentUserObject).child(rn).put(userProductImg).then(function(snapshot) {
    
          $timeout(function(){
               $scope.backgroundImageURL.push(snapshot.downloadURL);
              localStorage.setItem('userImageURL', $scope.backgroundImageURL);
           }, 50);
      })
     }
    

    【讨论】:

    • 先生,加载页面 imagURL 后获取但在主页图像中不再显示多次数组迭代,以防我只找到 1 个 imgeURL 检查我更新的控制台。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 2017-09-15
    • 2023-04-03
    • 2020-02-10
    相关资源
    最近更新 更多