我想從我的服務器api獲取一些json數據。網址是/ contacts。 我收到關於未知提供者的錯誤,但我不明白我在這裏做錯了什麼。我再次修改了代碼,但似乎再次發生。角度注射(未知供應商)
(function() {
var app = angular.module('test', ['ngRoute', 'Contacts', '$http']);
app.config(function($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider
.when("/", {
templateUrl: "partials/main.html",
controller: "contactsCtrl"
})
.otherwise({redirectTo:"/"});
});
app.service('Contacts', function($scope, $http) {
this.data = null;
this.all = function() {
$http.get('/contacts').
success(function(data) {
$scope.contacts = data;
})
};
});
app.controller('contactsCtrl', function($scope, Contacts) {
Contacts.all().query(function(data) {
$scope.contacts = data;
});
});
}());
<table class="table table-bordered">
<thead>
<th>Firstname <input type="search" class="pull-right"></th>
</thead>
<tbody>
<tr ng-repeat="contact in contacts">
<td>
<a ng-href="/edit/{{ contact.firstname }}">{{ contact.firstname }}</a>
</td>
</tr>
</tbody>
</table>
'Contacts'是服務,服務不能依賴於模塊,所以只需從這裏刪除'angular.module('test',['ngRoute','Contacts','$ http']);' – Grundy
非常感謝你 – pap