angular.js - 角度
怪我咯
怪我咯 2017-05-15 17:11:40
0
1
484

一個範例程式碼 運行了一下 報了一個expander undefined的錯誤?

<!DOCTYPE html>
<html ng-app="myapp">
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="js/angular.min.js"></script>
    <style>
        .expander{
            border:1px solid black;
            width:250px;
        }
        .expander>.title{
            background:black;
            cursor:pointer;
            color:#fff;
            padding:.1em .3em;
        }
        .expander>.body{
            padding:.1em .3em;
        }

    </style>
</head>
<body ng-controller="SomeController">
    <!--<p ng-controller="SomeController" >
        <expander class="expander" expander-title="title">
            {{text}}
        </expander>
    </p>-->
    <accordion>
        <expander class="expander" ng-repeat="expander in expanders" expander-title="expander.title">
                    {{expander.text}}
        </expander>
    </accordion>
</body>
<script>
    var appModule=angular.module("myapp",[]);
    appModule.directive("accordion",function(){
        return {
            restrict:"EA",
            replace:true,
            transclude:true,
            template:"<p ng-transclude></p>",
            controller:function(){
                var expanders=[];
                this.gotOpened=function(selectedExpander){
                    angular.forEach(expanders,function(expander){
                        if(selectedExpander!=expander){
                            expander.showMe=false;
                        }
                    });
                };
                this.addExpander=function(){
                    expanders.push(expander)
                }
            }
        }
    });
    appModule.directive("expander",function(){
        return {
            restrict:"EA",
            replace:true,
            transclude:true,
            require:"^?accordion",
            scope:{
                title:"=expanderTitle"
            },
            template:'<p>'+
                        '<p class="title" ng-click="toggle()">{{title}}</p>'+
                        '<p class="body" ng-show="showMe" ng-transclude></p>'+
                     '</p>',
            link:function(scope,element,attrs,accordionController){
                scope.showMe=false;
                accordionController.addExpander(scope);
                scope.toggle=function (){
                    scope.showMe=!scope.showMe;
                    accordionController.gotOpened(scope);
                }
            }
        }
    });
    appModule.controller("SomeController",function($scope){
        $scope.expanders=[
            {
                title:"Click me to expand",
                text:"Hi everyOne Jp;lkas;dkpoqawjd",
            },
            {
                title:"Click this",
                text:"I am even olasjmdaoliwjdzsl;dm",
            },
            {
                title:"Click me",
                text:"I am Kapsdojpoajsmfpo"
            }
        ];
    })
//    function SomeController($scope){
//        $scope.title="Click me to expand";
//        $scope.text="Hi there folks,I am the cotent";
//    }
</script>
</html>
怪我咯
怪我咯

走同样的路,发现不同的人生

全部回覆(1)
阿神

雷雷

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板