错误:[$ resource:badcfg],我该如何解决?

时间:2015-05-23 13:08:17

标签: json angularjs angular-ui-bootstrap

我刚刚开始使用棱角分明的东西,这是我的服务:

(function() {

    'use strict';

    angular
        .module('timeTracker')
        .factory('time', time);

        function time($resource) {

            // ngResource call to our static data
            var Time = $resource('data/time.json');

            function getTime() {
                // $promise.then allows us to intercept the results
                // which we will use later
                return Time.query().$promise.then(function(results) {
                    return results;
                }, function(error) { // Check for errors
                    console.log(error);
                });
            }

            return {
                getTime: getTime,
            }
        } 

' data / time.json'预先填好,我还没有后端系统 这是我的控制者:

(function() {

    'use strict';

    angular
        .module('timeTracker')
        .controller('TimeEntry', TimeEntry);

        function TimeEntry(time) {

            // vm is our capture variable
            var vm = this;

            vm.timeentries = [];

            // Fetches the time entries from the static JSON file
            // and puts the results on the vm.timeentries array
            time.getTime().then(function(results) {
                vm.timeentries = results;
                console.log(vm.timeentries);
            }, function(error) { // Check for errors
                console.log(error);
            });

        }
})();  

到目前为止,我只是试图在index.html文件中将所有内容连接在一起,但出现了问题。
我的索引的主体看起来像这样:

<body ng-app="timeTracker" ng-controller="TimeEntry as vm">

    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" href="#">Time Tracker</a>
            </div>
        </div>
        <div class="container-fluid time-entry">
            <div class="timepicker">
                <span class="timepicker-title label label-primary">Clock In</span>
                <timepicker ng-model="vm.clockIn" hour-step="1" minute-step="1" show-meridian="true">
                </timepicker> 
            </div>
            <div class="timepicker">
                <span class="timepicker-title label label-primary">Clock Out</span>
                <timepicker ng-model="vm.clockOut" hour-step="1" minute-step="1" show-meridian="true">
                </timepicker>
            </div>
            <div class="time-entry-comment">                
                <form class="navbar-form">
                    <input class="form-control" ng-model="vm.comment" placeholder="Enter a comment">
                    </input>
                    <button class="btn btn-primary" ng-click="vm.logNewTime()">Log Time</button>
                </form>
            </div>    
        </div>
    </nav>

    <div class="container">
        <div class="col-sm-8">
            <div class="well timeentry" ng-repeat="time in vm.timeentries">
                <div class="row">
                    <div class="col-sm-8">
                        <h4><i class="glyphicon glyphicon-user"></i> 
                        {{time.user_firstname}} {{time.user_lastname}}</h4>
                        <p><i class="glyphicon glyphicon-pencil"></i> {{time.comment}}</p>                  
                    </div>
                    <div class="col-sm-4 time-numbers">
                        <h4><i class="glyphicon glyphicon-calendar"></i> 
                        {{time.end_time | date:'mediumDate'}}</h4>
                    </div>
                </div>
            </div>
        </div>
    </div>

</body>  

无论如何,到目前为止,我无法找到一种方法来完成这项工作。我在控制台日志中一直收到同样的错误:

[Error] Error: [$resource:badcfg] Error in resource configuration for action `query`. Expected response to contain an array but got an object
http://errors.angularjs.org/1.3.15/$resource/badcfg?p0=query&p1=array&p2=object
http://localhost/TimeTracker/bower_components/angular/angular.js:63:32
http://localhost/TimeTracker/bower_components/angular-resource/angular-resource.js:587:40
processQueue@http://localhost/TimeTracker/bower_components/angular/angular.js:13248:29
http://localhost/TimeTracker/bower_components/angular/angular.js:13264:39
$eval@http://localhost/TimeTracker/bower_components/angular/angular.js:14466:28
$digest@http://localhost/TimeTracker/bower_components/angular/angular.js:14282:36
$apply@http://localhost/TimeTracker/bower_components/angular/angular.js:14571:31
done@http://localhost/TimeTracker/bower_components/angular/angular.js:9698:53
completeRequest@http://localhost/TimeTracker/bower_components/angular/angular.js:9888:15
requestLoaded@http://localhost/TimeTracker/bower_components/angular/angular.js:9829:24
    (anonymous function) (angular.js, line 11655)
    (anonymous function) (angular.js, line 8596)
    processQueue (angular.js, line 13256)
    (anonymous function) (angular.js, line 13264)
    $eval (angular.js, line 14466)
    $digest (angular.js, line 14282)
    $apply (angular.js, line 14571)
    done (angular.js, line 9698)
    completeRequest (angular.js, line 9888)
    requestLoaded (angular.js, line 9829)  

因此,拆分索引的正文,一切都在<nav>标记内正常工作,但"container"没有出现任何一个点。

2 个答案:

答案 0 :(得分:1)

在您的代码中,您忘记了几行

None

缺少以下几行:

(function() {

    'use strict';

    angular
        .module('timeTracker')
        .factory('time', time);

        function time($resource) {

            // ngResource call to our static data
            var Time = $resource('data/time.json');

            function getTime() {
                // $promise.then allows us to intercept the results
                // which we will use later
                return Time.query().$promise.then(function(results) {
                    return results;
                }, function(error) { // Check for errors
                    console.log(error);
                });
            }

            return {
                getTime: getTime,
            }
        } 

工作代码: http://embed.plnkr.co/RSIBLVHB8HZ03xPAa0nZ

答案 1 :(得分:0)

'use strict';不允许隐式依赖声明。试试

angular
        .module('timeTracker')
        .factory('time', ['$rosource', time]);

angular
    .module('timeTracker')
    .controller('TimeEntry', ['time', TimeEntry]);

或删除'use strict';