![](/img/trans.png)
[英]How to bind angular directive with ng-repeat to Owl Carousel
[英]angular js ng-repeat is not working with owl carousel
我正在Angular網站的新聞更新部分中工作,在這里我想顯示有關owl carousel的最新新聞更新,但是ng-repeat什么也不顯示。 json數據顯示在控制台中,但不在前面。
我的代碼如下:
<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();
});
我在JSFiddle中對其進行了測試: https ://jsfiddle.net/smoki99/rqwz6mno/12/
經過大量測試:
$ scope.allData正確的返回值。 通常,“數據”包含一個附加的數據字段。 不幸的是,我不知道您的確切“ 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();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.