使用Django的Angularjs:使用角色控制器创建http.get,并在网址

时间:2016-03-25 12:59:11

标签: angularjs django angular-controller django-angular

我的django观点如下:

#view to retrieve all the exercises
def exercise_all(request):
"""
List all exercises.
"""
if request.method == 'GET':
    exercises = Exercise.objects.all()
    serializer = ExerciseSerializer(exercises, many=True)
    return JSONResponse(serializer.data)


#view to retrieve initial default list of exercises, this will be called on angular controller load
def exercise_initial(request):
"""
Gives the first exercise.
"""
if request.method == 'GET':
    exercises = Exercise.objects.filter(exercise_type=1 , intensity_level=1)
    serializer = ExerciseSerializer(exercises, many=True)
    return JSONResponse(serializer.data)


#view to retrieve all specific exercises
def exercise_next(request, exType, level):
"""
Gives the next exercise.
"""
if request.method == 'GET':
    exercises = Exercise.objects.filter(exercise_type=exType, intensity_level=level)
    serializer = ExerciseSerializer(exercises, many=True)
    return JSONResponse(serializer.data)  

我的网址:

 url(r'^exercises/all/$', views.exercise_all),
 url(r'^exercises/initial/$', views.exercise_initial),
 url(r'^exercises/(?P<exType>\d)/(?P<level>\d)/$', views.exercise_next), 

我的角度控制器调用这些视图:

.controller('videoCtrl', function($scope,$http, $sce, $ionicPopup, djangoAuth) {

$scope.exercise_type='';
$scope.exercise_description='';
$scope.exercise_video_url='';

/**  To fetch the initial exercise **/
$http.get("https://localhost:8000/api/exercises/initial/").then(function(response){

  $scope.initial_data=response.data;

  angular.forEach($scope.initial_data, function(item){

               $scope.exercise_type=item.exercise_type;
               $scope.intensity_level=item.intensity_level;
               $scope.exercise_description=item.description;
                  $scope.exercise_video_url=$sce.trustAsResourceUrl(item.video_url);
           })
});

//Function to play next video
$scope.playNextTest = function(){

  alert("Next exercise: " + ($scope.exercise_type+1)); //demo line, logic needs to be implemented here     
}

我想要实现的目标:

  • 在初始视频控制器加载时,我可以获取我分配给$ scope参数的初始练习详细信息,如代码段中所示。
  • 当我运行playNextTest()函数时,我应该可以调用django view exercise_next ,我需要修改我的url,并附加类似../api/exercises/($scope.exercise_type)的内容1)/(($ scope.intensity_level)/

如何实现这一目标?

0 个答案:

没有答案