【问题标题】:AngularJs: Unknown provider: $resourceProvider <- $resource <- DonorerCtrlAngularJs:未知提供者:$resourceProvider <- $resource <- DonorerCtrl
【发布时间】:2015-11-03 09:10:14
【问题描述】:

我正在尝试为我的 Angular 应用程序加载“捐助者”列表。我从 API 获取数据,但没有按照我想要的方式工作。我收到此错误:

$resourceProvider

在我的 DonorerCtrl 类中,我有以下代码:

angular.module("testApp").controller("DonorerCtrl",
function($scope, $http, $resource) {
    console.log("Test fra donorerCtrl");

    $scope.donorerResource = $resource("http://bloodadmin.cloudapp.net/api/users/:donorid"),
    {id: "@donorid"}, { update: {method: "PUT"}};

    $scope.donorerVisits = $scope.donorerResource.query();

    $http (
        {
            method: "GET",
            url: "http://bloodadmin.cloudapp.net/api/donors"
        }).success(function(data) {
            $scope.donorerVisits = data;
        }).error(function() {
            alert("error");
        });

    $scope.donorerVisits = [];

});

我尝试使用以下代码将其加载到我的 HTML 文件中:

<div class="form-group col-lg-12">
    <table class="table">
        <thead>
        <tr>
            <th>Fornavn</th>
            <th>Efternavn</th>
            <th>Køn</th>
            <th>Fødselsdag</th>
            <th>Læs mere</th>
        </tr>
        </thead>
        <tbody>
        <tr ng-repeat="visit in donorerVisits">
            <td>{{visit.firstname}}</td>
            <td>{{visit.lastname}}</td>
            <td>{{visit.sex}}</td>
            <td>{{visit.age}}</td>
            <td><button class="btn btn-default">Læs mere</button></td>
        </tr>
        </tbody>
    </table>
</div>

在我的 Index html 中,我加载了这些 JS 文件:

<!-- JS -->
<script src="libs/angular.js" type="text/javascript"></script>
<script src="libs/angular-ui-router.min.js" type="text/javascript"></script>
<script src="libs/angular-resource.js" type="text/javascript"></script>

<!-- Angular Custom -->
<script type="text/javascript">
    angular.module("testApp", ['ui.router']);
</script>

<script src="js/controllers/HjemCtrl.js"></script>
<script src="js/controllers/DonorerCtrl.js"></script>
<script src="js/controllers/BlodCtrl.js"></script>
<script src="js/navigation.js"></script>

【问题讨论】:

    标签: html angularjs api resources angular-resource


    【解决方案1】:

    尝试:

    angular.module("testApp", ['ui.router', 'ngResource']);
    

    【讨论】:

    • 谢谢,似乎有效,但现在我有这个错误:index.html:1 XMLHttpRequest cannot load bloodadmin.cloudapp.net/api/donors。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问 Origin 'localhost:63342'。
    • 您需要在服务器端启用CORS
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2019-10-20
    相关资源
    最近更新 更多