2014-04-27 29 views
0

我有一個休息框架工作,序列化程序工作得很好。 api.py文件按以下格式顯示json。Django與角度js服務的休息框架連接

URL: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" 
     } 
    }] 

我也有當前從我手動創建的JSON文件中讀取靜態數據的角度的js服務:

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數據。我如何實現這一目標?

回答

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

+0

喜這樣做u能幫助我使API服務一個很好的相關計算器的問題嗎?我對角度有點新。 – Abhishek