0

I am studying the Localization example at the bottom of AngularJS home page.

I am looking at the source of the page and try to do exactly what the demo shows me.

However, I can't seem to fire up the demo code. The lines that confused me the most are:

   <span class="pull-right" 
    js-fiddle="tabs.html components.js beers.js" module="components"> ...
   <div app-run="tabs.html" module="components-us" class="well"> ...

Because I don't recognise app-run or js-fiddle to be AngularJS syntax (or is it?). Anyway, the demo app doesn't fire up. Could you please help me take a look?

Here is the code I COPY & PASTE from AngularJS home page and put in index.html: (change the header base and libraries to your directory accordingly)

index.html

<!doctype html>
<html ng-app>
    <head>
        <meta charset="utf-8">
        <!-- ************IMPORTANT !! change this to your directory************ -->
        <base href='http://localhost/angularjs/localization/' />
        <link rel="stylesheet" href="../bootstrap/css/bootstrap.css">
        <script src="../bootstrap/js/bootstrap.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js"></script>
    </head>
    <body>

    <div class=" row example">
        <div class="span1 app-source" app-source="tabs.html components.js beers.js" annotate="tabs.annotation" module="components"></div>
        <div class="span4">
            <span hint></span>
            <span class="pull-right" js-fiddle="tabs.html components.js beers.js" module="components"></span>
            <div class="tabs-spacer"></div>
            <h4>Locale: {{ 'US' }}</h4>
            <div app-run="tabs.html" module="components-us" class="well"></div>
            <div class="tabs-spacer"></div>
            <h4>Locale: {{ 'SK' }}</h4>
            <div app-run="tabs.html" module="components-sk" class="well"></div>
        </div>
    </div>

    <script>
        angular.module('components-us', ['components', 'ngLocal.us']);
        angular.module('components-sk', ['components', 'ngLocal.sk']);
    </script>

    <script type="text/ng-template" id="tabs.html">
        <tabs>
            <pane title="Localization">
                Date: {{ '2012-04-01' | date:'fullDate' }} <br>
                Currency: {{ 123456 | currency }} <br>
                Number: {{ 98765.4321 | number }} <br>
            </pane>
            <pane title="Pluralization">
                <div ng-controller="BeerCounter">
                    <div ng-repeat="beerCount in beers">
                        <ng-pluralize count="beerCount" when="beerForms"></ng-pluralize>
                    </div>
                </div>
            </pane>
        </tabs>
    </script>

    <script id="beers.js">
        function BeerCounter($scope, $locale) {
            $scope.beers = [0, 1, 2, 3, 4, 5, 6];
            if ($locale.id == 'en-us') {
                $scope.beerForms = {
                    0: 'no beers',
                    one: '{} beer',
                    other: '{} beers'
                };
            } else {
                $scope.beerForms = {
                    0: 'žiadne pivo',
                    one: '{} pivo',
                    few: '{} pivá',
                    other: '{} pív'
                };
            }
        }
    </script>

    <script id="components.js">
        angular.module('components', []).
                directive('tabs', function() {
                    return {
                        restrict: 'E',
                        transclude: true,
                        scope: {},
                        controller: function($scope, $element) {
                            var panes = $scope.panes = [];

                            $scope.select = function(pane) {
                                angular.forEach(panes, function(pane) {
                                    pane.selected = false;
                                });
                                pane.selected = true;
                            }

                            this.addPane = function(pane) {
                                if (panes.length == 0) $scope.select(pane);
                                panes.push(pane);
                            }
                        },
                        template:
                                '<div class="tabbable">' +
                                        '<ul class="nav nav-tabs">' +
                                        '<li ng-repeat="pane in panes" ng-class="{active:pane.selected}">'+
                                        '<a href="" ng-click="select(pane)">{{pane.title}}</a>' +
                                        '</li>' +
                                        '</ul>' +
                                        '<div class="tab-content" ng-transclude></div>' +
                                        '</div>',
                        replace: true
                    };
                }).
                directive('pane', function() {
                    return {
                        require: '^tabs',
                        restrict: 'E',
                        transclude: true,
                        scope: { title: '@' },
                        link: function(scope, element, attrs, tabsCtrl) {
                            tabsCtrl.addPane(scope);
                        },
                        template:
                                '<div class="tab-pane" ng-class="{active: selected}" ng-transclude>' +
                                        '</div>',
                        replace: true
                    };
                })
    </script>

   </body>
</html>

Also, where are app-run and js-fiddle from? I can't figure out if they are features belong to AngularJS or what.

4

1 に答える 1

1

app-runjs-fiddleは、カスタムの Angular ディレクティブです。つまり、AngularJS ホームページの実行を支援するために作成されたディレクティブです (したがって、これらが適切な homepage.js という名前のファイルにある理由です)。

これは、デモの動作するplnkrです。

于 2013-01-31T16:15:47.190 に答える