2017-02-17 132 views
1

由於某種原因,我沒有在瀏覽器中顯示數據。我在瀏覽器中看不到ng-repeat數據Angular js

這裏我的控制檯的PIC:

enter image description here

內部p標籤,我期待看到檢索到的數據。

這裏是視圖:

<div class="row" ng-repeat="voce in voices"> 
    <p style="color:#000;">{{voce.series}}</p> 
</div> 

這裏是控制器:

app.controller('MainCtrl', function($scope,$q,voci) { 

var promise = voci.getVoices(); 

promise.then(function(data){ 
    $scope.voices = data; 
    console.log($scope.voices); 
}); 

}); 

這裏是服務VOCI:

app.service('voci', function ($http,$q) { 

var deferred = $q.defer(); 

$http.get('urldata.json').then(function(data) 
{ 
    deferred.resolve(data); 
}); 

this.getVoices = function(){ 
    return deferred.promise; 
}; 

}) 

的輸出的console.log $ scope.voices :

enter image description here

我使用ngroute到視圖和控制器相關聯,主要模塊:

var app = angular.module('generaPreventivoApp', [ 
'ngAnimate', 
'ngCookies', 
'ngResource', 
'ngRoute', 
'ngSanitize' 
    ]) 

    .config(function ($routeProvider, $locationProvider) { 

$routeProvider 
    .when('/', { 
    templateUrl: 'views/main.html', 
    controller: 'MainCtrl', 
    controllerAs: 'main' 
    }) 
    .when('/about', { 
    templateUrl: 'views/about.html', 
    controller: 'AboutCtrl', 
    controllerAs: 'about' 
    }) 
    .otherwise({ 
    redirectTo: '/' 
    }); 
    $locationProvider.hashPrefix(''); 
    }); 

爲什麼這樣的問題?謝謝

+0

您是否獲得在'的console.log($ scope.voices)任何輸出;'? –

+0

數據是什麼樣的?如果沒有[mcve] – charlietfl

+0

,那麼$ http.get是否會從promise中返回數據?你是否在標記中聲明瞭控制器? – flashjpr

回答

2

嘗試:

<div class="row" ng-repeat="voce in voices.data"> 
    <p style="color:#000;">{{voce.series}}</p> 
</div> 

或:

app.controller('MainCtrl', function($scope,$q,voci) { 

var promise = voci.getVoices(); 

promise.then(function(data){ 
    $scope.voices = data.data; 
    console.log($scope.voices); 
}); 

}); 
+0

他們都工作!謝謝! –

+0

高興地幫助你! –