0

ここに私のコードがあります、

HTML:

<script type="text/html" id="test_temp">
    <div class="row" id="opportunityList">
        <% _.each(ops, function(option){
            <div class="span12">
                <div class="well basicInformation">
                    <div class="row">
                        <div class="span4 opportunityName" >
                            <h4><%= option.company_name %></h4>
                        </div>
                        <div class="span4 pull-right">
                            <ul class="inline">
                                <li class="dealGrade">
                                    <span><%= option.dealGrade %></span>
                                    <span class="subheader">Deal Grade</span>
                                </li>
                                <li class="estimateddevices">
                                    <span><%= option.devices %></span>
                                    <span class="subheader">Devices</span>
                                </li>
                                <li class="accountValue">
                                    <span><%= option.accountValue %></span>
                                    <span class="subheader">Account value</span>
                                </li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        });
        %>
    </div>
</script>

脚本:

testView = Backbone.View.extend({
    initialize: function(){
        this.render();
    },

    render: function(){
        var ops = [
            {dealGrade: '50%', devices: 123, accountValue: '20%', company_name: 'Kyocera', rep_name: 'James Kogg', rep_designation: 'Sales Rep', proposalCount: 2},
            {dealGrade: '75%', devices: 215, accountValue: '41%', company_name: 'Flipkart', rep_name: 'Christina Kogg', rep_designation: 'MD', proposalCount: 0}
        ]

        var template = _.template($("#test_temp").html(), ops);

        this.$el.append(template);
    }
});

var test_view = new testView({ el: $("#viewport .container") });

次のエラー メッセージが表示されます。

キャッチされない SyntaxError: 予期しないトークン < underscore.js 内

私は何を間違っていますか?

4

2 に答える 2

4

<%の周りを閉じたり、の終了_.each用のテンプレート タグを開いたりしていません。_.each});

<script type="text/html" id="test_temp">
   <div class="row" id="opportunityList">
     <% _.each(ops, function(option){ %>
       ...
     <% }); %>
   </div>
</script>

Underscore のテンプレート エンジンは非常に単純です。単純なテキスト ラングリングを実行して、テンプレートを裏返しに JavaScript コードに変換するだけです。

また、テンプレート関数はそのデータをキーと値のペア (つまり JavaScript オブジェクト) として必要とするためops、名前を付ける必要があります。

var template = _.template($("#test_temp").html(), { ops: ops });
于 2013-01-16T06:06:51.460 に答える