0

我正在尝试做的事情: 我正在使用 Angular UI-Router 构建一个单页应用程序。我的一个页面有一个折线图,所以我使用的是angular-chart.js

我不明白的: 我不明白如何让图表显示在 UI-Router 状态中。如果我不将它包含在单页应用程序中,我可以让图表工作。我感觉我需要向 UI-Router 状态添加一个控制器,其中包含 $scope 标签、系列和数据,但我无法让它正常工作。

Angular-Chart.js 代码

var chartApp = angular.module('myApp', ['chart.js']);

chartApp.controller("LineCtrl", function ($scope) {
    'use strict';   
    $scope.labels = ["January", "February", "March", "April", "May", "June", "July"];
    $scope.series = ['Motivation', 'Workload'];
    $scope.data = [
        [65, 59, 80, 81, 56, 55, 40],
        [28, 48, 40, 19, 86, 27, 90]
    ];
    $scope.onClick = function (points, evt) {
        console.log(points, evt);
    };
});

UI-路由器代码

var myApp = angular.module('myApp', ['ui.router']);

myApp.config(function($stateProvider, $urlRouterProvider) {
    'use strict';

    // For any unmatched url, redirect home
    $urlRouterProvider.otherwise('/home');

    $stateProvider

    // chart page
    .state('charts', {
        url: '/charts',
        templateUrl: 'app/charts.html'
    });
});
4

2 回答 2

1

在您的 javascript 代码中,您没有包含控制器。它应该是:

var myApp = angular.module('myApp', ['ui.router']);

myApp.config(function($stateProvider, $urlRouterProvider) {
    'use strict';

    // For any unmatched url, redirect home
    $urlRouterProvider.otherwise('/home');

    $stateProvider

    // chart page
    .state('charts', {
        url: '/charts',
        templateUrl: 'app/charts.html'
        controller: 'LineCtrl'
    });
});

虽然,有比这更好的方法。您可以解析来自服务的数据,以便页面在显示已编译的 html 之前等待该数据。

var myApp = angular.module('myApp', ['ui.router', 'someService']);

myApp.config(function($stateProvider, $urlRouterProvider) {
    'use strict';

    // For any unmatched url, redirect home
    $urlRouterProvider.otherwise('/home');

    $stateProvider

    // chart page
    .state('charts', {
        url: '/charts',

        //I use the 'views' property to ready my code for nested views as well as to
        //isolate the properties for each view on that particular url
        views: {
            '':{
                templateUrl: 'app/charts.html',
                controller: 'LineCtrl',
                controllerAs: 'lineCtrl',
                resolve:{
                    labels: function(someService){
                        return someService.getAllLabels();
                    },
                    series: function(someService){
                        return someService.getAllSeries();
                    },
                    data: function(someService){
                        return someService.getAllData();
                    }
               }
            }
        }
    });
});

你的控制器应该是这样的:

var chartApp = angular.module('myApp', ['chart.js']);

chartApp.controller("LineCtrl", ['$scope', 'labels', 'series', 'data', function ($scope, labels, series, data) {
    'use strict';   
    $scope.labels = labels;
    $scope.series = series;
    $scope.data = data;
    $scope.onClick = function (points, evt) {
        console.log(points, evt);
    };

}]);
于 2015-08-02T14:27:55.530 回答
0

我无法让 Immanuel 的代码为我工作,但他带领我走上了正确的轨道来使用视图。我从以下链接使用 UI 路由器阅读了多个视图:https ://scotch.io/tutorials/angular-routing-using-ui-router 。几乎立刻,我就可以让它工作了。

chart.html 页面称为 ui-view:

<div class="row"> <div class="col-md-6 col-sm-12" id="line-chart" ui-view="line-chart"></div> <div class="col-md-6 col-sm-12" id="bar-chart" ui-view="bar-chart"></div> </div> <div class="row"> <div class="col-md-6 col-sm-12" id="doughnut-chart" ui-view="doughnut-chart"></div> <div class="col-md-6 col-sm-12" id="radar-chart" ui-view="radar-chart"></div> </div>

我的 app.js 代码:

var myApp = angular.module('myApp', ['ui.router', 'chart.js']);
myApp.config(function($stateProvider, $urlRouterProvider) {
    'use strict';

    // For any unmatched url, redirect home
    $urlRouterProvider.otherwise('/home');

    $stateProvider

    // charts page
    .state('charts', {
        url: '/charts',
        views: {
            '': { templateUrl: 'app/charts.html', },
            'line-chart@charts': {
                templateUrl: 'app/charts-line.html',
                controller: 'lineController'
            },
            "bar-chart@charts": {
                templateUrl: 'app/charts-bar.html',
                controller: 'barController'
            },
            "doughnut-chart@charts": {
                templateUrl: 'app/charts-doughnut.html',
                controller: 'doughnutController'
            },
            "radar-chart@charts": {
                templateUrl: 'app/charts-radar.html',
                controller: 'radarController'
            }
        }
    })
});

然后我为每个人打电话给控制器。这是折线图:

// Define the charts controller called from the charts state
myApp.controller('lineController', function($scope) {
    'use strict';
    $scope.message = 'There should be a line chart here:';
    $scope.labels = ["January", "February", "March", "April", "May", "June", "July"];
    $scope.series = ['Motivation', 'Workload'];
    $scope.data = [
        [65, 59, 80, 81, 56, 55, 40],
        [28, 48, 40, 19, 86, 27, 90]
    ];
});

就是这样!

于 2015-08-03T21:51:10.680 回答