我正在调用基于类的视图

class CurrentUser(APIView):
    authentication_classes = (authentication.TokenAuthentication,)
    def get(self, request, format=None):
        for user in User.objects.all()
            if request.user.is_authenticated():
                fullname = user.get_full_name()
                return Response(fullname)


从网址

url(r'^currentuser$', views.CurrentUser.as_view(), name='current-user'


我正在获取当前用户名。但是我想通过以下方式将该数据带入angularjs控制器的$ scope中..请..任何帮助将不胜感激..

controllersModule.controller('homeCtrl', function($scope, $http, User , Product ,Process ,Ingredient) {

        $scope.$on('event:login-confirmed', function() {
            $scope.users = User.query();
            $scope.products = Product.query();
            $scope.processes = Process.query();
            $scope.ingredients = Ingredient.query();
            // $scope.currentUser = get;
        });

});

最佳答案

我认为User是服务吗?如果您使用$ http或Restangular,您可能会得到一个应许。因此,您最终会在范围内获得承诺,这可能不是您想要的。

因此,您可能想要这样:

$scope.$on('event:login-confirmed', function() {
            User.query().then(function(data) { $scope.users = data;});
            ...
        });

关于javascript - 从Django的数据中获取angularjs的$ scope中的数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19995240/

10-09 23:41
查看更多