angularjs与jquery特效slidetoggle结合

之前使用jquery的slidetoggle方法,可以轻易的实现元素的收缩展开;使用angularjs后,没有找到相关的方法,通过多方面查资料,自己写了个demo,展示slidetoggle的angularjs写法:

html

<div ng-controller="ctrl_main">
    <li ng-repeat-start="el in list" ng-click="$index=!$index">{{el.name}}</li>
    <li ng-repeat-end slide-toggle="$index">{{$index}}</li>
</div>

控制器ctrl_main

app.controller('ctrl_main', function ($scope) {
        $scope.list=[{name:'lewis'},{name:'susan'},{name:'alice'},{name:'jay'}]
    });

指令drtv_slidetoggle

app.directive('slideToggle', function() {
        return {
            restrict: 'A',
            scope:{
                isOpen: "=slideToggle"
            },
            link: function(scope, element, attr) {
                scope.$watch('isOpen', function(newVal,oldVal){
                    if(newVal !== oldVal){
                        element.stop().slideToggle('slow');
                    }
                });
            }
        };
    });

运行效果:

点击其他人名,也是一样的。

总体思路就是编写指令,通过改变指令的值,来控制当前元素的收缩展开。

 

===不用requirejs的同学就可以不往下看了===

【ps】将该指令用requirejs封装成指令模块,引用就可以在你的项目中使用了;

requirejs封装的指令模块,新建js,命名为drtv_slidetoggle.js:

define(['app','jquery'], function (app) {
    app.directive('slideToggle', function() {
        return {
            restrict: 'A',
            scope:{
                isOpen: "=slideToggle"
            },
            link: function(scope, element, attr) {
                scope.$watch('isOpen', function(newVal,oldVal){
                    if(newVal !== oldVal){
                        element.stop().slideToggle('slow');
                    }
                });
            }
        };
    });
});

在启动app时候调用:

require(['angular', 'domReady!','ctrl_main','drtv_slidetoggle'], function (angular) {
            angular.bootstrap(document, ['app'])
        });

 

转载于:https://my.oschina.net/u/3699676/blog/1544970

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
< DOCTYPE html> <html ang app ""> <head> < <link rel "stylesheet" href "http: apps bdimg com libs bootstrap 3 2 0 css bootstrap min css"> > < head> <body ng controller "userController"> <div class "container"> <h3>Users< h3> <table class "table table striped"> <thead><tr> <th>Edit< th> <th>First Name< th> <th>Last Name< th> < tr>< thead> <tbody><tr ng repeat "user in users"> <td> <button class "btn" ng click "editUser user id "> < <span class "glyphicon glyphicon pencil">< span>&nbsp;&nbsp;Edit > < button> < td> <td>{{ user fName }}< td> <td>{{ user lName }}< td> < tr>< tbody> < table> <hr> <button class "btn btn success" ng click "editUser "new" "> <span class "glyphicon glyphicon user">< span> Create New User < button> <hr> <h3 ng show "edit">Create New User:< h3> <h3 ng hide "edit">Edit User:< h3> <form class "form horizontal"> <div class "form group size13"> <label style "float:left" class "col sm 2 control label">Email地址:< label> <div class "col sm 10"> <input style "height: 30px;width: 330px;" type "text" ng model "fName" > < div> < div> <div class "form group"> <label style "float:left" class "col sm 2 control label">Last Name:< label> <div class "col sm 10"> <input style "height: 30px;width: 330px;" type "text" ng model "lName" placeholder "Last Name"> < div> < div> <div class "form group"> <label style "float:left" class "col sm 2 control label">Password:< label> <div class "col sm 10"> <input style "height: 30px;width: 330px;" type "password" ng model "passw1" placeholder "Password"> < div> < div> <div class "form group"> <label style "float:left" class "col sm 2 control label">Repeat:< label> <div class "col sm 10"> <input style "height: 30px;width: 330px;" class "size14 "type "password" ng model "passw2" placeholder "Repeat Password"> < div> < div> < form> <hr> <div> <button class "btn btn success" ng disabled "error || incomplete"> 更新 < button> < div> <script src "http: apps bdimg com libs angular js 1 2 15 angular min js">< script> <script src "myUsers js"> function userController $scope { $scope fName ""; $scope lName ""; $scope passw1 ""; $scope passw2 ""; $scope users [ {id:1 fName:"Hege" lName:"Pege" } {id:2 fName:"Kim" lName:"Pim" } {id:3 fName:"Sal" lName:"Smith" } {id:4 fName:"Jack" lName:"Jones" } {id:5 fName:"John" lName:"Doe" } {id:6 fName:"Peter" lName:"Pan" } ]; $scope edit true; $scope error false; $scope incomplete false; $scope editUser function id { if id "new" { $scope edit true; $scope incomplete true; $scope fName ""; $scope lName ""; } else { $scope edit false; $scope fName $scope users[id 1] fName; $scope lName $scope users[id 1] lName; } }; $scope $watch "passw1" function {$scope test ;} ; $scope $watch "passw2" function {$scope test ;} ; $scope $watch "fName" function {$scope test ;} ; $scope $watch "lName" function {$scope test ;} ; $scope test function { if $scope passw1 $scope passw2 { $scope error true; } else { $scope error false; } $scope incomplete false; if $scope edit && $scope fName length || $scope lName length || $scope passw1 length || $scope passw2 length { $scope incomplete true; } }; } < script> < body> < html>>">< DOCTYPE html> <html ang app ""> <head> < <link rel "stylesheet" href "http: apps bdimg com libs bootstrap 3 2 0 css bootstrap min css"> > < head> <body ng controller "userController"> <div class "con [更多]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值