bootstrap-switch和angularjs结合使用

由于angularjs的dom操作总是先执行,导致$(input[name="switch"])找不到元素,所以使用directive自定义指令,有两种方法:

html部分:

 <my-input power="{{x.power}}" did="{{x.id}}"></my-input>

directive指令部分:

1、通过插入元素的方法

app.directive('myInput',function(factoryName){
return{
restrict : "AE",
scope : {
power :"@",
did : "@"
},
template :'<div class="switch"></div>',
replace : true,
link : function(scope,element,attr){
var $input = $('<input type="checkbox" name="switch" checked>');
$(element[]).append($input);
$(element[]).children().bootstrapSwitch({
'size':'small',
onSwitchChange : function(target,state){
//state是开关的状态
}
})
}
}
})

2、通过引入外部文件的方法

 return{
template : '<div ng-include="getElement()"></div>',
scope : {
dtype : '@',
id : '@'
},
replace : true,
link : function(scope,element,attr){
scope.getElement = function(){
if(attr.dtype == 3){
return 'testchecked.html';
}else{
return 'testunchecked.html';
}
};
}
}

外部文件testchecked.html为

<div class="switch" data-on="info" data-off="success">
<input type="checkbox" name="switch" checked/>
</div>
<script type="text/javascript">
$("input[name='switch']").bootstrapSwitch({
'size' : 'normal',
'onColor':'info',
'onSwitchChange':function(target,state){ }
})
</script>

bootstrap-switch与angularjs结合使用的更多相关文章

  1. bootstrap switch功能

    bootstrap switch是一个按钮开关,点击时获取其状态可通过以下代码: <input id="email_switch_state" type="chec ...

  2. 前端插件之Bootstrap Switch 选择框开关控制

    简介 Bootstrap Switch是一款轻量级插件,可以给选择框设置类似于开关的样式 它是依赖于Bootstrap的一款插件 下载 下载地址 在线引用 导入 因为它是依赖于Bootstrap的一款 ...

  3. bootstrap switch样式修改与多列等间距布局

    先以一张图开启今天的随笔 今天实习遇到了switch按钮,小姐姐说用插件bootstrap switch来写,我第一次用这个插件,首先在引入方面就遇到了很多坑,先来总结一下bootstrap swit ...

  4. 前端基础(十):Bootstrap Switch 选择框开关控制

    简介 Bootstrap Switch是一款轻量级插件,可以给选择框设置类似于开关的样式 它是依赖于Bootstrap的一款插件 下载 下载地址 在线引用 导入 因为它是依赖于Bootstrap的一款 ...

  5. 使用jQuery获取Bootstrap Switch的值

    $('#switcher').bootstrapSwitch('state'); // true || false $('#switcher').bootstrapSwitch('toggleStat ...

  6. bootstrap Switch 的一个坑点

    在bootstrap的modal点开的时候改变bootstrapSwitch的状态的时候,会出现第一次打开modal,switch没有变化,第二次以后打开modal才会改变,这个问题找了好久没有找到答 ...

  7. bootstrap tooltips在 angularJS中的使用

    使用bootstrap自带的提示控件,省去了不少事情 <div class="s2" ng-init="InitTooltip()"> <in ...

  8. Bootstrap switch 切换状态踩坑

    Boostrap switch 下载地址(http://www.bootcss.com/p/bootstrap-switch/),同时配有一些简单的用例. 其中写到 Toggle State切换状态的 ...

  9. AngularJS 参考手册

    AngularJS 参考手册 AngularJS 指令 本教程用到的 AngularJS 指令 : 指令 描述 ng-app 定义应用程序的根元素. ng-bind 绑定 HTML 元素到应用程序数据 ...

  10. [译]用AngularJS构建大型ASP.NET单页应用(一)

    原文地址:http://www.codeproject.com/Articles/808213/Developing-a-Large-Scale-Application-with-a-Single 渣 ...

随机推荐

  1. 用java进行简单的万年历编写

    import java.util.Scanner; public class PrintCalendarDemo1 { public static void main(String[] args) { ...

  2. WebService有什么用?

    入门之前先简单介绍下WCF.在用WebService做开发时,很多人都不知道WCF和WebService之间的关系.实际上WCF包含了WebService,这是一个很强悍的通信技术应用框架.微软把.N ...

  3. Spring笔记①--helloworld

    Spring Spring是一个轻量级控制反转(IoC)和面向切面(AOP)的容器框架,它主要是为了解决企业应用开发的复杂性而诞生的: 目的:解决企业应用开发的复杂性 功能:使用基本的Javabean ...

  4. 配置树莓派/Linux默认声卡设备

    1.设置默认声卡为USB声卡 在$HOME下新建.asoundrc $cd $HOME $nano .asoundrc 输入以下内容 defaults.ctl.card 1 defaults.pcm. ...

  5. Teamwork(The third day of the team)

    在确定了第一个spring后我们就开始了各自的工作,不过由于大家都在专注于自己的工作并且由于近段时间的作业及各方面的事情都很多,没有来得及每天都更新一个博客,因此,我们现在把落下的博客都补上,很多事情 ...

  6. mvc 路由配置-学习

    MapRoute(RouteCollection, String, String) 映射指定的URL路由. 'Declaration <ExtensionAttribute> _ Publ ...

  7. VMware上配置DPDK环境并运行实例程序

    1. 在虚拟机VMware上配置环境 VMware安装:http://www.zdfans.com/html/5928.html Ubuntu:https://www.ubuntu.com/downl ...

  8. build.xml

    下载ant 解压ant 后设置ANT_HOME, PATH中添加ANT_HOME目录下的bin目录(如:ANT_HOME:,PATH:D:\apache-ant-1.9.2%ANT_HOME%\bin ...

  9. 函数调用时形参的传递也会被认为是赋值操作(继承自Object后会出现的问题)

    http://blog.csdn.net/houqd2012/article/details/25070987

  10. BZOJ5118 Fib数列2(矩阵快速幂)

    特殊矩阵的幂同样满足费马小定理. #include<iostream> #include<cstdio> #include<cmath> #include<c ...