0

我正在研究 AngularJS 主页底部的本地化示例。

我正在查看页面的来源,并尝试完全按照演示显示的内容进行操作。

但是,我似乎无法启动演示代码。最让我困惑的台词是:

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

因为我不认识app-run或者js-fiddle是 AngularJS 语法(或者是吗?)。无论如何,演示应用程序不会启动。你能帮我看看吗?

这是我从 AngularJS 主页复制和粘贴并放入 index.html 的代码:(相应地将标头库和库更改为您的目录)

索引.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>

另外,app-run从哪里来js-fiddle?我不知道它们是属于 AngularJS 的功能还是什么。

4

1 回答 1

1

app-runjs-fiddle是自定义的 Angular 指令——即,他们编写的用于帮助运行 AngularJS 主页的指令(因此它们位于名为 homepage.js 的文件中)。

这是演示的工作 plnkr

于 2013-01-31T16:15:47.190 回答