Angular.js可以轻松切换JSON文件

时间:2015-02-08 12:32:54

标签: javascript json angularjs

我有一个单个JSON文件的工作示例,现在我想添加几个类似的JSON文件,以便我可以在它们之间切换。

的script.js:

var app = angular.module('app', []);

app.controller('AppCtrl', ['$scope', '$http', function(scope, http){

    scope.mode = 'official';

    // dict = [{'name': 'official', 'file': 'schedule314_official.json'}, {'name': 'personal', 'file': 'schedule314.json'}];
    dict = {'official': 'schedule314_official.json', 'personal': 'schedule314.json'}
    // http.get('schedule314_official.json').success(function(data){
    http.get(dict[scope.mode]).success(function(data){
        scope.days = data;
        scope.latex = "";
        var sday, lesson;
        for(var i = 0; i < scope.days.length; i++) {
            sday = scope.days[i];
            scope.latex += "    {\\begin{Large} " + sday.day + "  \\end{Large}  \n";
            scope.latex += "        \\begin{table}[!h]\n        \\begin{tabular}{ p{3cm}  p{10cm} p{5cm} }  \n";
            for(var j = 0; j < sday.lessons.length; j++) {
                lesson = sday.lessons[j];
                scope.latex += "            " + lesson.start + " - " + lesson.end + " & " + lesson.subj + " & " + lesson.place + "\\\\\n";
            }
            scope.latex += "        \\end{tabular}\n        \\end{table} \\\\\n";
        }
    });
}]);

的index.html:

...
<ul class="nav nav-pills">
    <li role="presentation"><a href="#" ng-click="$parent.mode='official';">Official</a></li>
    <li role="presentation"><a href="#" ng-click="$parent.mode='personal';">Personal</a></li>
</ul>
...

0 个答案:

没有答案