0

尝试运行本文中解释的示例

我的html

    <!doctype html>
<html lang="en" ng-app="myApp">
<head>
    <meta charset="utf-8">
    <title>Form</title>
    <link rel="stylesheet" href="css/app.css">
    <!--<link rel="stylesheet" href="css/bootstrap.css">-->
    <script src="http://code.jquery.com/jquery.js"></script>

    <script src="lib/angular/angular.js"></script>
    <link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
    <link href="css/bootstrap-responsive.css" rel="stylesheet">
    <script src="js/dir.js"></script>
    <script>

    </script>
</head>
<body>

    {{2+2}}

    <!-- Invoke the directive -->
    <div ng-sparkline ng-city="San Francisco"></div>
    {{weather}}

<script src="js/bootstrap.min.js"></script>

</body>
</html>

文章末尾的js代码:

// Code goes here
var app = angular.module('myApp', []);

app.directive('ngSparkline', function() {
    var url = "http://api.openweathermap.org/data/2.5/forecast/daily?mode=json&units=imperial&cnt=14&callback=JSON_CALLBACK&q=";
    return {
        restrict: 'A',
        require: '^ngCity',
        transclude: true,
        scope: {
            ngCity: '@'
        },
        template: '<div class="sparkline"><div ng-transclude></div><div class="graph"></div></div>',
        controller: ['$scope', '$http', function($scope, $http) {
            $scope.getTemp = function(city) {
                $http({
                    method: 'JSONP',
                    url: url + city
                }).success(function(data) {
                        var weather = [];
                        angular.forEach(data.list, function(value){
                            weather.push(value);
                        });
                        $scope.weather = weather;
                    });
            }
        }],
        link: function(scope, iElement, iAttrs, ctrl) {
            scope.getTemp(iAttrs.ngCity);
            scope.$watch('weather', function(newVal) {
                // the `$watch` function will fire even if the
                // weather property is undefined, so we'll
                // check for it
                if (newVal) {
                    var highs = [];

                    angular.forEach(scope.weather, function(value){
                        highs.push(value.temp.max);
                    });

                    chartGraph(iElement, highs, iAttrs);
                }
            });
        }
    }
});

var chartGraph = function(element, data, opts) {
    var width = opts.width || 200,
        height = opts.height || 80,
        padding = opts.padding || 30;

    // chart
    var svg     = d3.select(element[0])
        .append('svg:svg')
        .attr('width', width)
        .attr('height', height)
        .attr('class', 'sparkline')
        .append('g')
        .attr('transform', 'translate('+padding+', '+padding+')');

    svg.selectAll('*').remove();

    var maxY    = d3.max(data),
        x       = d3.scale.linear()
            .domain([0, data.length])
            .range([0, width]),
        y       = d3.scale.linear()
            .domain([0, maxY])
            .range([height, 0]),
        yAxis = d3.svg.axis().scale(y)
            .orient('left')
            .ticks(5);

    svg.append('g')
        .attr('class', 'axis')
        .call(yAxis);

    var line    = d3.svg.line()
            .interpolate('linear')
            .x(function(d,i){return x(i);})
            .y(function(d,i){return y(d);}),
        path    = svg.append('svg:path')
            .data([data])
            .attr('d', line)
            .attr('fill', 'none')
            .attr('stroke-width', '1');
}

app.directive('ngCity', function() {
    return {
        controller: function($scope) {}
    }
});

据我了解,代码应替换放置的女巫“ng-sparkline”指令中的“div”。但它什么也没做(

jsfiddle 与我的尝试 http://jsfiddle.net/dRSR8/

你能帮我找出我做错了什么吗?谢谢!

4

1 回答 1

1

两件事情:

而不是包括AngularJS“onLoad”,而是将它放置在“No Wrap - in head”(或“No Wrap - in body”也可以)。您将在“框架和扩展”下的左侧看到此选项

然后,由于您使用的是 D3,请在外部资源下添加:http ://d3js.org/d3.v3.min.js

于 2013-10-12T00:19:22.090 回答