javascript - angularjs:这段示例代码逻辑上有点看不懂
PHPz
PHPz 2017-04-11 12:26:28
[JavaScript讨论组]

下面这段代码有注释的地方是我不懂的地方,还请大神帮我解释下,这个是个tab切换的效果,如果愿意的话还请帮忙解释下这里面的逻辑,万分感谢!!!

directive('tabs', function() {

        return {
            restrict: 'E',
            transclude: true,
            scope: {},
            controller: ["$scope", function($scope) {//为什么是数组呢:"$scope"该怎么理解呢
                var panes = $scope.panes = [];
                
                $scope.select = function(pane) {
                    angular.forEach(panes, function(pane) {
                        pane.selected = false;
                    });
                    pane.selected = true;
                }

                this.addPane = function(pane) {//这里的this该怎么解释,为什么能直接this.addPane呢
                    if(panes.length == 0) $scope.select(pane);
                    panes.push(pane);
                }
            }],
            template: '

' + '

' + '

' + '

', replace: true }; }). directive('pane', function() { return { require: '^tabs',// restrict: 'E', transclude: true, scope: { title: '@' }, link: function(scope, element, attrs, tabsCtrl) { tabsCtrl.addPane(scope);//这里为什么传scope参数呢 }, template: '

' +//ng-class="{active: selected}"怎么理解呢 '

', replace: true }; })
PHPz
PHPz

学习是最好的投资!

全部回复(2)
怪我咯

你的问题太多了,你需要好好看一下Angular的模型、作用域(scope)、数据绑定。
讲道理学习JS框架从Demo学起并不是一个很好的方式,应该先了解一下框架的原理(DOM渲染、数据绑定、事件传递)。

PHP中文网

`return {

        restrict: 'E',
        transclude: true,
        scope: {},
        controller: ["$scope", function($scope) {//数组是要注入$scope吧,$scope是指令作用域
            var panes = $scope.panes = [];
            
            $scope.select = function(pane) {
                angular.forEach(panes, function(pane) {
                    pane.selected = false;
                });
                pane.selected = true;
            }

            this.addPane = function(pane) {//这里的this应该是controller
                if(panes.length == 0) $scope.select(pane);
                panes.push(pane);
            }
        }],
        template: '<p 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>' +//ng-click="select(pane)"这里面的参数pane是从哪里来的呢,难道是ng-repeat里面可以直接定义,这里面可以直接传?
            '</li>' +
            '</ul>' +
            '<p class="tab-content" ng-transclude></p>' +
            '</p>',
        replace: true
    };
}).
directive('pane', function() {
    return {
        require: '^tabs',//
        restrict: 'E',
        transclude: true,
        scope: {
            title: '@'
        },
        link: function(scope, element, attrs, tabsCtrl) {
            
            tabsCtrl.addPane(scope);//这里为什么传scope参数呢
        },
        template: '<p class="tab-pane" ng-class="{active: selected}" ng-transclude>' +//ng-class="{active: selected}"当selected为true时就class等于active
            '</p>',
        replace: true
    };
})`指令是最难的,我用angularjs几个月了还不会自己写指令
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号