buttonsRadio directivies

本实例主要展示怎样用directives实现buttons radio。

AngularButtonDirective.html

<!doctype html>
<html lang="en" ng-app="BJButtionDirective">
<head>
    <script src="lib/jquery-1.9.1.js"></script>
    <script src="lib/angular.js"></script>
    <script type="text/javascript" src="js/buttons-radio-controller.js"></script>
</head>
<body ng-controller="MainCtrl">
<div class="container">
    <div class="span6">
        <br>
        <strong>myModel: </strong>
        <code> {{myModel}} </code>
        <hr>
        <div>
            <input name="test" type="radio" ng-model="myModel" value="Left"> Left<br>
            <input name="test" type="radio" ng-model="myModel" value="Middle"> Middle<br>
            <input name="test" type="radio" ng-model="myModel" value="Right"> Right<br>
        </div>
        <hr>
        <buttons-radio class="btn-group" data-toggle="buttons-radio" model='myModel' options='myOptions'></buttons-radio>
    </div>
</div>
</body>
</html>

buttons-radio-controller.js

'use strict';
function MainCtrl($scope) {
    $scope.myOptions = ['Left', 'Middle', 'Right'];
    $scope.myModel = 'Left';
    $scope.$watch('myModel', function(v){
        console.log('changed', v);
    });
}

angular.module('BJButtionDirective', []).directive('buttonsRadio', function() {
    return {
        restrict: 'E',
        scope: {
        	model: '=', 
        	options:'='
        },
        controller: function($scope){
            $scope.activate = function(option){
                $scope.model = option;
            };
        },
        template: "<button type='button' class='btn' "+
            "ng-class='{active: option == model}'"+
            "ng-repeat='option in options' "+
            "ng-click='activate(option)'>{{option}} "+
            "</button>"
    };
});

 运行效果:

点击单选项,模型发生改变

点击指令写的buttons radio,效果一样

控制台输出信息如下:

实际我们在开发过程中,在指令中一般用link,即buttons-radio-controller.js内容如下:

'use strict';
function MainCtrl($scope) {
    $scope.myOptions = ['Left', 'Middle', 'Right'];
    $scope.myModel = 'Left';
    $scope.$watch('myModel', function(v){
        console.log('changed', v);
    });
}

angular.module('BJButtionDirective', []).directive('buttonsRadio', function() {
    return {
        restrict: 'E',
        scope: {
        	model: '=', 
        	options:'='
        },
        link: function(scope, el, attrs){
        	scope.activate = function(option){
        		scope.model = option;
			};
        },
        template: "<button type='button' class='btn' "+
            "ng-class='{active: option == model}'"+
            "ng-repeat='option in options' "+
            "ng-click='activate(option)'>{{option}} "+
            "</button>"
    };
});

对buttonsRadio directives的解释:
     restrict:代码中我们使用了"E"属性,表示创建此directive将创建一个element
     scope:一般用于从controller中获取$scope中的一些值或进行数据的通信

         a.'='表示指向同一个引用,即指令中对模型的改变,会直接改变到指令外面同模型

         b.‘@表示使用此指令时,使用的地方向指令中传递值给指令,它是单向的,只支持String

         c.事件通知,指令通知外面

当然,上面指令在兼容IE8和IE8以下的版本中不能正常运行,需修改元素标签指令为属性标签,如下所示:

AngularButtonDirective.html

<!doctype html>
<html lang="en" ng-app="BJButtionDirective">
<head>
    <script src="lib/jquery-1.9.1.js"></script>
    <script src="lib/angular.js"></script>
    <script type="text/javascript" src="js/buttons-radio-controller.js"></script>
</head>
<body ng-controller="MainCtrl">
<div class="container">
    <div class="span6">
        <br>
        <strong>myModel: </strong>
        <code> {{myModel}} </code>
        <hr>
        <div>
            <input name="test" type="radio" ng-model="myModel" value="Left"> Left<br>
            <input name="test" type="radio" ng-model="myModel" value="Middle"> Middle<br>
            <input name="test" type="radio" ng-model="myModel" value="Right"> Right<br>
        </div>
        <hr>
        <span buttons-radio="" class="btn-group" data-toggle="buttons-radio" model='myModel' options='myOptions'></span>
    </div>
</div>
</body>
</html>

 buttons-radio-controller.js

'use strict';
function MainCtrl($scope) {
    $scope.myOptions = ['Left', 'Middle', 'Right'];
    $scope.myModel = 'Left';
    $scope.$watch('myModel', function(v){
        console.log('changed', v);
    });
}

angular.module('BJButtionDirective', []).directive('buttonsRadio', function() {
    return {
        restrict: 'A',
        scope: {
        	model: '=', 
        	options:'='
        },
        link: function(scope, el, attrs){
        	scope.activate = function(option){
        		scope.model = option;
			};
        },
        template: "<button type='button' class='btn' "+
            "ng-class='{active: option == model}'"+
            "ng-repeat='option in options' "+
            "ng-click='activate(option)'>{{option}} "+
            "</button>"
    };
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值