2017-09-20 109 views
0

我正在使用我的Angular網站的新聞更新部分,我想用owl carousel顯示最新的新聞更新,但是ng-repeat不顯示任何內容。 json數據在控制檯中顯示,但不在前面。角js ng-repeat不適用於貓頭鷹旋轉木馬

我的代碼如下:

<div id="owl-news" ng-controller="newsController" data-ng-init="news()"> 

    <div class="item" ng-repeat="news in allData"> 
     <div class="news-1"> 

      <div class="body"> 

      <p>{{ news.news}}</p> 
      <div class="title">{{news.news_date}}</div> 

      </div> 
     </div> 
    </div> 

</div> 

角碼:

app.controller("newsController",function($http,$scope,$log){ 
var news=function(){ 
    $http({ 
      method:"POST", 
      url:"getNews.php", 
      dataType:"json" 
      }).then(function(data){ 
       $log.log(data); 
       $scope.allData=data; 
       },function(data){ 
        $log.log("Error has occured!."); 
       }); 
    }; news(); 

     }); 

回答

0

我測試了它的jsfiddle:

$:https://jsfiddle.net/smoki99/rqwz6mno/12/

做了很多的測試之後scope.allData正確的返回值。通常「數據」包含一個額外的數據字段。我unfortunably不知道,你確切的「JSON」回報是什麼樣子,但它把它從這個源(http://www.dysartpharmacy.com/index.php/site_search/get/news

所以我認爲你必須將線改變只是爲了

$scope.allData = data.data; 

這裏我的完整例如:

<div ng-app="myApp" ng-controller="newsController"> 
    <div class="item" ng-repeat="news in allData"> 
    <div class="news-1"> 
     <div class="body"> 
     <p>{{ news.news}}</p> 
     <div class="title">{{news.news_date}}</div> 
     </div> 
    </div> 
    </div> 
</div> 

和JS代碼:

// the main (app) module 
var app = angular.module("myApp", []); 

// add a controller 
app.controller("newsController",function($http,$scope,$log, $httpParamSerializerJQLike){ 
    var returnData = '[{"news":"news1","news_date":"2017-09-14"},   {"news":"news2","news_date":"2017-09-14"}]'; 

    var data = $httpParamSerializerJQLike({ 
     json: returnData 
    }); 

    var news = function() { 
     $http({ 
      method: "POST", 
      url: "/echo/json/", 
      datatype: "json", 
      data: data 
      }, data).then(function (data) { 
      debugger; 
      $log.log(data); 
       $scope.allData = data.data; 
      },function(data) { 
      $log.log("Error has occured!"); 
      }); 
    };  
    news(); 

}); 
+0

感謝立即響應。你的代碼運行良好,但貓頭鷹旋轉木馬會產生一些問題。它將所有數據放入一張幻燈片中。任何suggesssion –

+0

如果是這樣的:https://owlcarousel2.github.io/OwlCarousel2/docs/api-classes.html 比也許你需要重新安排divs並設置爲正確的「類」 –

相關問題