【问题标题】:Django rest framework connection with angular js servicesDjango rest 框架与 Angular js 服务的连接
【发布时间】:2014-06-12 16:47:45
【问题描述】:

我有一个正在工作的休息框架,并且序列化程序工作得很好。 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"
        }
    }]

我还有一个 Angular 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 数据,而不是从静态数据中读取。我如何做到这一点?

【问题讨论】:

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


    【解决方案1】:

    我建议为与您的 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 问题

    【讨论】:

    • 嗨你能帮我做api服务吗?我对角度有点陌生。
    猜你喜欢
    • 1970-01-01
    • 2020-05-15
    • 2015-08-07
    • 2015-07-16
    • 2020-01-19
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 2011-01-30
    相关资源
    最近更新 更多