我有一個在後端使用Django開發的應用程序,其中創建了一個可以使用angular讀取前端信息的API。
我用$ http調用數據庫中的人員列表,在函數中獲取數據,並用console.log顯示並且是正確的,但是當我使用ng-repeat在模板中調用該信息時,內容div創建,但裏面的信息不顯示,保持空白。
我會很感激任何解決這個問題的方法。
//定製angular.js
var app = angular.module('SesamoApp', []);
var URL = '/api/projects/torres-de-monterrey/segmentation/';
app.controller('cardsCtrl',['$scope', '$http', function($scope, $http) {
$scope.cards = [
{email: '[email protected]', segment: 'low'}
];
$http({
method: 'GET',
url: URL
}).then(function successCallback(response) {
// this callback will be called asynchronously
// when the response is available
/*console.log(response.data);*/
$scope.data = response.data;
angular.forEach(response.data, function(value, key){
console.log(key + ': ' + value.email + ' -- ' + value.segment);
$scope.cards.push({email: value.email, segment: value.segment});
});
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
console.log($scope.cards);
}]);
// list.html
{% extends "base_app.html" %}
{% block main-container %}
<h1>Actividades de hoy</h1>
{% if activities %}
{% for activity in activities %}
{{activity.activity_type}}
{% endfor %}
{% else %}
<p>No tienes actividades programadas para hoy</p>
<hr />
<div class="segmentBlocks">
<div ng-app="SesamoApp">
<div class="cards" ng-controller="cardsCtrl">
<div class="card" ng-repeat="card in cards | orderBy:'segment'">
<p class="email">{{ card.email }}</p>
<p class="segment">{{ card.segment }}</p>
</div>
{{ data }}
</div>
</div>
</div>
{% endif %}
{% endblock %}
截圖與執行後的結果(無信息)
'的console.log(鍵+ ':' + value.email + ' - ' + value.segment);'做它在控制檯中重現呢? – developer033
是的,顯示所有信息正確 –