Angularjs指令链接功能

时间:2016-02-23 05:30:30

标签: angularjs

html -



<html ng-app="myApp">
<head>
    <title></title>
    <link type="text/css" rel="stylesheet" href="style.css"/>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.min.js"></script>
    <script src="script.js"></script>
</head>

  <div ng-controller="mainCtrl">
    <div class="login-div">
        <div id="div1" class="login toggle">
            <input type="text" placeholder="username" />
            <input type="password" placeholder="password" />
            <button class="btn-login">Login</button>
            <div class="link">
                <p id="p1" ng-click="toggleClick()">Click here</p>
            </div>
        </div>
        <div id="div2" class="sign-up toggle">
            <input type="text" placeholder="username" />
            <input type="email" placeholder="email" />
            <input type="password" placeholder="password
            <button class="btn-login">Sign up</button>" />
            <div class="link">
                <p id="p2" ng-click="toggleClick()">Click here again</p>
            </div>
        </div>
        <div id="div3" class="sign-up toggle">
            <input type="text" placeholder="username" />
            <input type="email" placeholder="email" />
            <input type="password" placeholder="password
            <button class="btn-login">Sign up</button>" />
            <div class="link">
                <p id="p3" ng-click="toggleClick()">Click here again</p>
            </div>
        </div>
        <div class="btn-close">
            <button>X</button>
        </div>
    </div>
  </div>
</html>
&#13;
&#13;
&#13;

js file -

&#13;
&#13;
var myApp = angular.module("myApp", []);

myApp.controller('mainCtrl', function($scope, $rootScope) {
  //$scope.toggleClick = function(){
  //  $rootScope.$broadcast("toogleDiv","");
  //};
});

myApp.directive('toggle', function () {
    return {
        restrict:'C',
        link: function (scope, element, attrs) {
           
            scope.toggleClick = function(){
              alert(element.attr('id'));
              element.slideToggle();
            };
        }                  
    }
});
&#13;
&#13;
&#13;

无论是哪个段落项目我点击,第三个div只切换。但是,如果您看到toggle指令在所有div上。所以基本上所有div都应该滑动,但只有最后一个才能滑动。

style.css -

&#13;
&#13;
.login-div{
    margin: 50px;
    width: 200px;
    padding:10px;
    background-color: black;
    box-shadow: 0px 0px 15px 1px rgba(50, 50, 50, 1);
    position: relative;
}

.login, .sign-up {
    padding:15px;
    background-color:#EFF7F7;
    border: solid 1px gray;
}

.btn-close {
    margin-top: 10px;
    text-align: right;
}

.btn-login{
    width: 100%;
    margin: 10px 0px;
}

input{
    border: 1px solid #ccc;
    border-radius: 4px;
    padding: 5px 0 5px 5px;
    color: #555;
    width: 162px;
}

a{
    display: inline-block;
    text-decoration: none;
}

link{
    text-align: right;
}



p{
  cursor: pointer;
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

在指令定义中添加scope:{},可以解决您的问题。您的指令需要一个隔离的范围才能重用它。

问题是toggleClick定义被分配给父作用域,基于你的html意味着div3指令的执行被覆盖并且是最后定义的函数。出于兴趣,如果你交换你的div,你可能会注意到最后定义的div总是被点击的。

Plunker:https://plnkr.co/edit/y5bD8yDZ0dLZZW6FlnFE