Django使用angular js服务休息框架连接

时间:2014-04-27 03:31:39

标签: javascript json django angularjs django-rest-framework

我有一个休息框架工作,序列化器工作得很好。 api.py文件将json显示为以下格式。

网址:http://127.0.0.1:8000 /api / studentacademicprograms /

[
    {
        "credits_completed": 32, 
        "academic_program_gpa": 3.7, 
        "primary_program": true, 
        "academic_program": {
            "acad_program_id": 124, 
            "acad_program_category": {
                "id": 1, 
                "program_type": 1, 
                "title": "Associate in Arts"
            }, 
            "acad_program_type": {
                "id": 2, 
                "program_type": "Certificate"
            }, 
            "acad_program_code": "AA.ARTS", 
            "program_title": "Associate in Arts Degree", 
            "required_credits": 60, 
            "min_gpa": 3.3, 
            "description": "another description"
        }
    }]

我还有一个角度js服务,当前正在从我手动创建的json文件中读取静态数据:

services.js:

angular.module('jsonService', ['ngResource'])
    .factory('DegreesService', function($resource) {
      return $resource('/static/json/degrees.json')
    })

    .factory('DegreeCategoriesService', function($resource) {
        return $resource('/static/json/degreecategories.json')
    })

    .factory('DetailsService', function($resource) {
        return $resource('/static/json/details.json')
    })

    .factory('ProgramsService', function($resource) {
        return $resource('/static/json/programs.json')
    });

但现在我想更改角度工厂,以便从其余框架获取json数据,而不是从静态数据中读取。我如何实现这一目标?

1 个答案:

答案 0 :(得分:0)

我建议为与API端点的交互提供服务 - 取决于服务的安静程度,考虑使用restangular之类的东西,这可以使事情变得更加容易。如果你只需要一个只读服务API,那么restangular将是矫枉过正的。

http://www.benlesh.com/2013/02/angularjs-creating-service-with-http.html有一个很好的一般性描述如何执行此操作,以及在Convert Angular HTTP.get function to a service执行此操作的一个很好的相关stackoverflow问题