AngularJS:应用程序无法从get请求中获取数据

时间:2013-10-08 05:21:10

标签: angularjs coffeescript pug

我的Angular应用程序具有以下代码:

index.jade:

doctype 5
html
    head

    body(ng-app='app')
        div(ng-controller='EventController')
            div(ng-repeat='event in events')
                h2 {{event.name}}
                h3 {{event.location}}

            h2 Create new event
            label(for='name') Name:
            input(ng-model='event.name')

            label(for='location') Location:
            input(ng-model='event.location')
            button(ng-click='create(event)') Submit

        script(src='https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js')
        script(src='https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular-resource.js')

        script(src= 'javascripts/app.js')

app.coffee:

app = angular.module 'app', ['ngResource']

EventsFactory = ($resource, $q)->
    url = 'http://localhost:3000/events'
    resource = $resource(url)
    resourceWithId = $resource("#{url}/:id", {id: '@id'})

    list = ()->
        deferred = $q.defer()
        resource.get(
            (events)-> 
                deferred.resolve events
            ,(response)-> 
                deferred.reject response 
            )
        return deferred.promise

    create = (event)->
        deferred = $q.defer()
        resource.save(
            event
            ,(response)->
                deferred.resolve response
            ,(response)->
                deferred.reject response
            )
        return deferred.promise  

    ret =  
        'list': list
        'create': create
    return ret

app.factory 'EventsFactory', EventsFactory


EventController = ($scope, EventsFactory)->
    $scope.list = ()->
        EventsFactory.list().then(
            (events)-> 
                return events
            ,(response)-> 
                console.log response
            )
    $scope.events = $scope.list()

    $scope.create = (event)->
        EventsFactory.create(event).then(
            (response)-> 
                console.log 'success', response
            ,(response)->
                console.log 'failure', response
            )
app.controller 'EventController', EventController

我收到以下错误:

GET http://localhost/events  angular.js:10419
(anonymous function) angular.js:10419
sendReq angular.js:10241
$http.serverRequest angular.js:10018
wrappedCallback angular.js:7509
wrappedCallback angular.js:7509
(anonymous function) angular.js:7582
Scope.$eval angular.js:8926
Scope.$digest angular.js:8789
Scope.$apply angular.js:9012
(anonymous function) angular.js:1004
invoke angular.js:2925
resumeBootstrapInternal angular.js:1002
bootstrap angular.js:1017
angularInit angular.js:977
(anonymous function) angular.js:16872
trigger angular.js:1812
(anonymous function) angular.js:2052
forEach angular.js:148
eventHandler

我做错了什么?

编辑:使用/ events而不是localhost:3000 / events会给我以下错误:

TypeError: Object #<Resource> has no method 'push'
    at copy (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:616:21)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular-resource.js:484:17
    at wrappedCallback (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:7509:59)
    at wrappedCallback (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:7509:59)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:7582:26
    at Object.Scope.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:8926:28)
    at Object.Scope.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:8789:23)
    at Object.Scope.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:9012:24)
    at done (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:10265:45)
    at completeRequest (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.js:10449:7) angular.js:6349

1 个答案:

答案 0 :(得分:1)

您希望resource.query()不是resource.get()query()检索结果列表(isArray: true),get()检索单个对象。在这种情况下,get不是HTTP方法,而是更多的检索操作类型。