最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Syntax Error: Token '{' invalid key at column 2 of the expression [{{id}}] starting at [{id}}]? - S

programmeradmin1浏览0评论

This is my code:

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="js/angular.js"></script>
    <script src="js/Chart.min.js"></script>
    <script src="js/angular-chart.js"></script>
</head>
<body>

<div ng-app="app">
    <div ng-controller="BarCtrl">
        <div ng-repeat="id in ids">
            <canvas id="bar" class="chart chart-bar"
                    chart-data="{{id}}" chart-labels="labels"> chart-series="series"
            </canvas>

        </div>
    </div>
</div>

<script type="application/javascript">

    angular.module("app", ["chart.js"]).controller("BarCtrl", function ($scope) {
        $scope.labels = ['2008', '2009', '2010', '2011', '2012'];
        $scope.series = ['Series A', 'Series B'];

        $scope.data = [];
        $scope.data.push( [
            [65, 59, 80, 81, 56, 55, 40],
            [28, 48, 40, 19, 86, 27, 90]
        ]);

        $scope.ids = ["data[0]"];
    });

</script>
</body>
</html>

This is my code:

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="js/angular.js"></script>
    <script src="js/Chart.min.js"></script>
    <script src="js/angular-chart.js"></script>
</head>
<body>

<div ng-app="app">
    <div ng-controller="BarCtrl">
        <div ng-repeat="id in ids">
            <canvas id="bar" class="chart chart-bar"
                    chart-data="{{id}}" chart-labels="labels"> chart-series="series"
            </canvas>

        </div>
    </div>
</div>

<script type="application/javascript">

    angular.module("app", ["chart.js"]).controller("BarCtrl", function ($scope) {
        $scope.labels = ['2008', '2009', '2010', '2011', '2012'];
        $scope.series = ['Series A', 'Series B'];

        $scope.data = [];
        $scope.data.push( [
            [65, 59, 80, 81, 56, 55, 40],
            [28, 48, 40, 19, 86, 27, 90]
        ]);

        $scope.ids = ["data[0]"];
    });

</script>
</body>
</html>

When I run the code in Chrome, the console wrote:

[$parse:syntax] Syntax Error:
    Token '{' invalid key at column 2 of the expression [{{id}}] starting at [{id}}].

<input value="{{id}}"> seems to be ok, but chart-data="{{id}}" is causing the Syntax Error. Any idea why that would happen?

Share Improve this question edited Feb 15, 2017 at 4:20 talemyn 7,9704 gold badges33 silver badges52 bronze badges asked Feb 15, 2017 at 4:12 wang mingwang ming 1992 silver badges9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

chart-data should not have expression, it should have a scope variable binding.

<canvas id="bar" class="chart chart-bar" chart-data="data" chart-labels="labels" chart-series="series">
</canvas>

{{ ... }} means you're evaluating an expression, for example {{1+1}} or printing (not passing) a variable {{id}}. However, to pass a variable, you should not wrap it in an expression when you're assigning it to a property.

发布评论

评论列表(0)

  1. 暂无评论