角度js中的自定义指令不起作用

时间:2016-05-11 11:44:20

标签: angularjs

App.directive('dExpandCollapse', function () {

    return {
        restrict: 'EA',
        link: function (scope, element, attrs) {
            $(element).click(function () {
                //var show = "false";
                $(element).find(".answer").slideToggle('200', function () {
                    // You may toggle + - icon     
                    $(element).find("span").toggleClass('faqPlus faqMinus');
                });


                if ($("div.answer:visible").length > 1) {
                    // You may toggle + - icon                      //$(this).parent().find("span.faqMinus").removeClass('faqMinus').addClass('faqPlus');
                    $(this).siblings().find(".answer").slideUp('slow');
                }
            });
        }
    }
});

并在html中调用它,如下所示:

<div ng-app="App">
    <d-expand-collapse>
        <div><span class="faqPlus"></span><a href="javascript:void(0)" class="question ui-link">Question1?</a></div>
        <div class="answer">
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </div>
    </d-expand-collapse>

    <d-expand-collapse>

        <div><span class="faqPlus"></span><a href="javascript:void(0)" class="question ui-link">Question2 ?</a></div>
        <div class="answer">
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </div>
    </d-expand-collapse>

    <d-expand-collapse>
        <div>
            <span class="faqPlus"></span><a href="javascript:void(0)" class="question ui-link">Question3?</a>
        </div>
        <div class="answer">
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </div>
    </d-expand-collapse>


    <d-expand-collapse>
        <div><span class="faqPlus"></span><a href="javascript:void(0)" class="question ui-link">Question 4?</a></div>
        <div class="answer">
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </div>
    </d-expand-collapse>


</div>

仍然没有用。

1 个答案:

答案 0 :(得分:0)

您的问题尚不完整。什么不起作用并不完全清楚。在我的一些假设下,我写了这个html页面。

<!DOCTYPE html>
<html ng-app="exampleApp">
    <head>
        <title>Example</title>
        <script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
        <script src="angular.min.js"></script>
        <script>
        angular.module('exampleApp', [])
        .directive('dExpandCollapse', function () {
            return {
                restrict: 'EA',
                link: function (scope, element, attrs) {
                    $(element).click(function () {
                        //var show = "false";
                        $(element).find(".answer").slideToggle('200', function () {
                            // You may toggle + - icon     
                            $(element).find("span").toggleClass('faqPlus faqMinus');
                        });


                        if ($("div.answer:visible").length > 1) {
                            // You may toggle + - icon                      //$(this).parent().find("span.faqMinus").removeClass('faqMinus').addClass('faqPlus');
                            $(this).siblings().find(".answer").slideUp('slow');
                        }
                    });
                }
            }
        });
        </script>
        <link href="bootstrap/css/bootstrap.css" rel="stylesheet" />
        <link href="bootstrap/css/bootstrap-theme.css" rel="stylesheet" />
    </head>
    <body>
        <d-expand-collapse>
        <div><span class="faqPlus"></span><a href="javascript:void(0)" class="question ui-link">Question1?</a></div>
        <div class="answer">
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </div>
    </d-expand-collapse>

    <d-expand-collapse>

        <div><span class="faqPlus"></span><a href="javascript:void(0)" class="question ui-link">Question2 ?</a></div>
        <div class="answer">
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </div>
    </d-expand-collapse>

    <d-expand-collapse>
        <div>
            <span class="faqPlus"></span><a href="javascript:void(0)" class="question ui-link">Question3?</a>
        </div>
        <div class="answer">
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </div>
    </d-expand-collapse>


    <d-expand-collapse>
        <div><span class="faqPlus"></span><a href="javascript:void(0)" class="question ui-link">Question 4?</a></div>
        <div class="answer">
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </div>
    </d-expand-collapse>
    </body>
</html>
它几乎送了一支手风琴。请提供有关您面临的确切问题的更多详细信息。