如何在html中通过django视图显示angularjs数据?

时间:2016-05-06 09:35:51

标签: html angularjs ajax django

我正在开发一款django + angularjs应用。我通过角度控制器通过django视图到html(仪表板)的ajax请求显示数据。 但是,当我尝试使用头文件执行相同的操作时,这是一个常见的文件,所有其他的html' s我无法实现这一点。我不知道我在哪里弄错了。

如果有人可以帮我解决这个问题,那么如何将一些仪表板数据显示到头文件中会很棒。

header.html 文件 -

<div ng-controller="DashboardController" class="page-header-inner">
    <div class="page-logo">
        <a href="index.html">
        <img src="static/assets/admin/layout4/img/phyzio_logo.png" alt="logo" class="logo-default"/>
        </a>
        <div class="menu-toggler sidebar-toggler">

        </div>
    </div>

    <a href="javascript:;" class="menu-toggler responsive-toggler" data-toggle="collapse" data-target=".navbar-collapse">
    </a>

    <div class="page-top">

        <div class="top-menu">
            <ul class="nav navbar-nav pull-right">
                <li class="separator hide"></li>

                <li class="separator hide"></li>

                <li class="separator hide"></li>

                <li class="dropdown dropdown-user dropdown-dark">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-close-others="true">
                        <span class="username username-hide-on-mobile">
                        {$ Name $}
                        </span>

                        <img alt="" src="{$ image_url $}"/>
                    </a>
                    <ul class="dropdown-menu dropdown-menu-default">
                        <li>
                            <a href="#/profile/dashboard">
                            <i class="icon-user"></i> My Profile </a>
                        </li>
                        <li>
                            <a href="#">
                            <i class="icon-calendar"></i> 2 devices </a>
                        </li>

                        <li>
                            <a href="#">
                            <i class="icon-key"></i> Log Out </a>
                        </li>
                    </ul>
                </li>

            </ul>
        </div>

    </div>

</div>

app.js 档案: -

    var MetronicApp = angular.module("MetronicApp", [
    "ui.router",
    "ui.bootstrap",
    "oc.lazyLoad",
    "ngSanitize",
    'ngCookies'
]);

    MetronicApp.config(function($interpolateProvider) {
    $interpolateProvider.startSymbol('{$');
    $interpolateProvider.endSymbol('$}');
});

    MetronicApp.config(function($httpProvider) {
    $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
    $httpProvider.defaults.xsrfCookieName = 'csrftoken';
    $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
});

MetronicApp.config(['$ocLazyLoadProvider', function($ocLazyLoadProvider) {
    $ocLazyLoadProvider.config({
        cssFilesInsertBefore: 'ng_load_plugins_before' // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
    });
}]);
    MetronicApp.factory('authInterceptor', ['$q', '$window', '$injector', function($q, $window, $injector) {
    return {
        request: function(config) {
            config.headers = config.headers || {};
            if ($window.localStorage.token) {
                config.headers.Authorization = 'Bearer ' + $window.localStorage.token;
            }
            return config;
        },
        response: function(response) {
            if (response.status === 401) {
                // handle the case where the user is not authenticated
            }
            return response || $q.when(response);
        },
        responseError: function(rejection) {
            if (rejection.status == 401) {
                var $state = $injector.get("$state");
                $state.go('login')
                    // location.reload();
            }
            return $q.reject(rejection);
        }
    };
}]);

    MetronicApp.config(['$httpProvider', function($httpProvider) {
    $httpProvider.interceptors.push('authInterceptor');
}]);
    MetronicApp.config(['$controllerProvider', function($controllerProvider) {

    $controllerProvider.allowGlobals();
}]);


MetronicApp.controller('AppController', ['$scope', '$rootScope', function($scope, $rootScope) {
    $scope.$on('$viewContentLoaded', function() {
        Metronic.initComponents(); // init core components

    });
}]);
    MetronicApp.controller('HeaderController', ['$scope', function($scope) {
    $scope.$on('$includeContentLoaded', function() {
        Layout.initHeader(); // init header
    });
}]);

/* Setup Layout Part - Sidebar */
MetronicApp.controller('SidebarController', ['$scope', function($scope) {
    $scope.$on('$includeContentLoaded', function() {
        Layout.initSidebar(); // init sidebar
    });
}]);

/* Setup Layout Part - Sideba
r */
MetronicApp.controller('PageHeadController', ['$scope', function($scope) {
    $scope.$on('$includeContentLoaded', function() {
        Demo.init(); // init theme panel
    });
}]);

/* Setup Layout Part - Footer */
MetronicApp.controller('FooterController', ['$scope', function($scope) {
    $scope.$on('$includeContentLoaded', function() {
        Layout.initFooter(); // init footer
    });
}]);

/* Setup Rounting For All Pages */
MetronicApp.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {

    // Redirect any unmatched url
    $urlRouterProvider
        .otherwise(function($injector, $location) {
            // This function prevents  the infinite loop on otherwise while session wasn't found
            var $state = $injector.get("$state");
            $state.go("home");
        });

    $stateProvider

        .state('app', {
            url: "/app/",
            templateUrl: "static/html/tpl/app.html"
        })

        .state('header', {
            url: "/header/",
            templateUrl: "static/html/tpl/header.html",
            controller: "DashboardController"
        })
        // Dashboard
        .state('app.dashboard', {
            url: "dashboard",
            templateUrl: "static/html/views/dashboard.html",
            data: {
                pageTitle: 'Dashboard'
                // pageSubTitle: 'statistics & reports'
            },
            requireLogin: true,
            controller: "DashboardController",
            resolve: {
                deps: ['$ocLazyLoad', function($ocLazyLoad) {
                    return $ocLazyLoad.load({
                        name: 'MetronicApp',
                        insertBefore: '#ng_load_plugins_before', // load the above css files before '#ng_load_plugins_before'
                        files: [
                            'static/assets/global/plugins/morris/morris.css',
                            'static/assets/admin/pages/css/tasks.css',
                            'static/assets/global/plugins/morris/morris.min.js',
                            'static/assets/global/plugins/morris/raphael-min.js',
                            'static/assets/global/plugins/angularjs/angular.min.js',
                            'static/assets/global/plugins/jquery-easypiechart/jquery.easypiechart.min.js',
                            'static/assets/global/plugins/bootstrap-hover-dropdown/bootstrap-hover-dropdown.min.js',
                            'static/assets/global/plugins/jquery.sparkline.min.js',
                            'static/assets/admin/pages/scripts/index3.js',
                            'static/assets/admin/pages/scripts/tasks.js',
                            'static/js/controllers/DashboardController.js',

                        ]
                    });
                }]
            }
        })

DashboardController.js 文件: -

'use strict';

MetronicApp.controller('DashboardController', function($rootScope, $scope, $http, $timeout) {
    $scope.$on('$viewContentLoaded', function() {   
        // initialize core components
        Metronic.initAjax();

        getdata();
    });


    function getdata(){
        var tsteps =  '';
        var tcalories = '';
        var tcadence = '';
        var tdistance = '';
        var tName = '';
        var tAge = '';
        var theight = '';
        var tweight = '';
        var tsex = '';
        var tTreatment_area = '';
        var tmode = '';
        var tduration = '';
        var timage = '';
        $.ajax({
            type: "GET",
            url: "/userdata/",
            async: false,

            success: function(response){
                tsteps = response.no_of_steps;
                tcalories = response.calories;
                tcadence = response.cadence;
                tdistance = response.distance;
                tName = response.Name;
                tAge = response.Age; 
                theight = response.height;
                tweight = response.weight;
                tsex = response.sex;
                tTreatment_area = response.treatment_area
                tmode = response.mode
                tduration = response.duration
                timage = response.image_url
                // callback(response);
            },

            error: function(){
              alert("error 2631");
            }
        });

        $scope.steps = tsteps;
        $scope.calories = tcalories;
        $scope.cadence = tcadence;
        $scope.distance = tdistance;
        $scope.Name = tName;
        $scope.Age =  tAge;
        $scope.height = theight;
        $scope.weight = tweight;
        $scope.sex = tsex;
        $scope.treatment_area = tTreatment_area;
        $scope.mode = tmode;
        $scope.duration = tduration;
        $scope.image_url = timage;

        console.log(12345, tsteps, tcalories, tcadence, tdistance, tName, tAge, theight, tweight, tsex, tTreatment_area, tmode, tduration, timage);
    }
});

我发布数据的ajax请求: -

$.ajax({
                                    method: 'POST',
                                    url: '/fblogin_app/',
                                    async: false,
                                    data: {
                                        email: email,
                                        picture: picture
                                    },
                                    // headers: {
                                    //     'X-CSRFTOKEN': $cookies.csrftoken
                                    // },
                                    success : function(data){

                                        // alert("sucess fb");
                                        if (data.status == true){

                                            window.location.href='/#/app/dashboard';

                                        }

                                        else {

                                       alert('Wrong Username/Password');
                                        }

                                    },

                                    error : function(){
                                        alert('error 6352');
                                    }
                                });

0 个答案:

没有答案