angularjs-ui插件ui-select和html的select注意事项及区别
项目中使用了angular-ui里的ui-select指令,地址https://github.com/angular-ui/ui-select
1. ng-model没有双向数据绑定
最开始没有看手册,直接仿照之前前辈写的ui-select,比如:
...
<ui-select ng-model="nameOld" theme="bootstrap" style="min-width: 300px;" name="oldTest" ng-change=change(nameOld)>
<ui-select-match placeholder="test old example">{{$select.selected.name}}</ui-select-match>
<ui-select-choices repeat="s in oldDatas | propsFilter: {name: $select.search, age: $select.search}">
<div ng-bind-html="s.name | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
...
这里在ng-change的函数里比如传入形参赋值给$scope.nameOld,才能形成双向数据绑定的效果。
...
$scope.change = function (testOld){
console.log($scope.nameOld); //undefined
$scope.nameOld = testOld;
console.log($scope.nameOld); //hello
}
...
后来在手册中发现可以使用selected来实现双向数据绑定
...
<ui-select ng-model="nameOld.selected" theme="bootstrap" style="min-width: 300px;" name="oldTest" ng-change=change()>
<ui-select-match placeholder="test old example">{{$select.selected.name}}</ui-select-match>
<ui-select-choices repeat="s in oldDatas | propsFilter: {name: $select.search, age: $select.search}">
<div ng-bind-html="s.name | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
...
对应的js中要先声明一个nameOld对象:
...
$scope.nameOld = {}; $scope.change = function (){ console.log($scope.nameOld.selected); //hello
}
...
或者使用$parent.xxx,我理解的是ui-select插件创建了一个自己的作用域,需要使用$parent来和父作用域进行绑定;
...
<ui-select ng-model="$parent.nameOld" theme="bootstrap" style="min-width: 300px;" name="oldTest" ng-change=change()>
<ui-select-match placeholder="test old example">{{$select.selected.name}}</ui-select-match>
<ui-select-choices repeat="s in oldDatas | propsFilter: {name: $select.search, age: $select.search}">
<div ng-bind-html="s.name | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
...
这时候js比较简单:
...
$scope.change = function (){ console.log($scope.nameOld); //hello
}
...
2. 下拉列表为多属性对象,想绑定的属性和展示的属性不是同一个
如果是一个对象数组,如下所示,这时候可以选择传入后台的数据是一个属性,还是一个数组元素
...
$scope.testArr = [
{
id: 1,
name: "a"
},
{
id: 2,
name: "b"
},
{
id: 3,
name: "c"
},
]; $scope.testChange = function () {
console.log($scope.testSelect);
console.log($scope.testSelect2);
}
...
对应的html如下:上为传入对象、下为传入属性值
...
/*select标签*/
<select ng-model="testSelect" ng-options="test.name for test in testArr" ng-change="testChange()"></select> <select ng-model="testSelect2" ng-options="test.name as test.name for test in testArr" ng-change="testChange()"></select> /*补充:ui-select指令里对象设置*/
/*单传属性*/
<ui-select ng-model="$parent.test" theme="bootstrap" style="min-width: 300px;" name="oldTest" ng-change=testChange()>
<ui-select-match placeholder="test old example">{{$select.selected.name}}</ui-select-match>
<ui-select-choices repeat="s.name as in testArr| propsFilter: {name: $select.search, age: $select.search}">
<div ng-bind-html="s.name | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
/*传对象*/
<ui-select ng-model="$parent.test" theme="bootstrap" style="min-width: 300px;" name="oldTest" ng-change=testChange()>
<ui-select-match placeholder="test old example">{{$select.selected.name}}</ui-select-match>
<ui-select-choices repeat="s in testArr| propsFilter: {name: $select.search, age: $select.search}">
<div ng-bind-html="s.name | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
...
angularjs-ui插件ui-select和html的select注意事项及区别的更多相关文章
- Struts2 easy UI插件
一.easy UI是类似于jQuery UI的插件库,它提供了丰富的各种常用插件:tree.datagrid... tree插件: 语法:$(selector).tree([settings]); 常 ...
- 【UI插件】简单的日历插件(下)—— 学习MVC思想
前言 我们上次写了一个简单的日历插件,但是只是一个半成品,而且做完后发现一些问题,于是我们今天尝试来解决这些问题 PS:距离上次貌似很久了 上次,我们大概遇到哪些问题呢: ① 既然想做一套UI库,那么 ...
- 【UI插件】开发一个简单日历插件(上)
前言 最近开始整理我们的单页应用框架了,虽然可能比不上MVVM模式的开发效率,也可能没有Backbone框架模块清晰,但是好歹也是自己开发出来 而且也用于了这么多频道的东西,如果没有总结,没有整理,没 ...
- Unreal Engine 4 Radiant UI 插件入门教程(二)
本篇章前提要求:在UE4上安装了Radiant UI插件.如果没有安装,请找其它教程(或者是笔者的其它的教程,目前正在写). 本教程的目的:探讨如何从网页元素中调用蓝图中的内容: 第一步: 写一个网页 ...
- target-densitydpi=device-dpi会使其他ui插件布局变小
target-densitydpi=device-dpi会使其他ui插件布局变小 东哥说:不用rem了,把meta改成这样<meta name="viewport" cont ...
- 基于AngularJS的Onsen UI --Onsen UI学习笔记
AngularJS与Onsen UI的结合,Onsen UI应用程序实际上是一个AngularJS 1应用程序. <!doctype html><html lang="en ...
- amazeui-js插件-ui增强-日期组件如何使用(把实例做一下)
amazeui-js插件-ui增强-日期组件如何使用(把实例做一下) 一.总结 一句话总结:需要jquery.js和amazeui.js一切才能使用 1.amazeui中的各种js效果要怎么才能使用? ...
- 转AngularJS路由插件
AngularJS学习笔记--002--Angular JS路由插件ui.router源码解析 标签: angular源码angularjs 2016-05-04 13:14 916人阅读 评论(0) ...
- 重大发现: windows下C++ UI库 UI神器-SOUI(转载)
转载:http://www.cnblogs.com/setoutsoft/p/4996870.html 在Windows平台上开发客户端产品是一个非常痛苦的过程,特别是还要用C++的时候.尽管很多语言 ...
- 转: windows下C++ UI库 UI神器-SOUI
转:http://www.cnblogs.com/setoutsoft/p/4996870.html 前言 在Windows平台上开发客户端产品是一个非常痛苦的过程,特别是还要用C++的时候.尽管很多 ...
随机推荐
- 18 A GIF decoder: an exercise in Go interfaces 一个GIF解码器:go语言接口训练
A GIF decoder: an exercise in Go interfaces 一个GIF解码器:go语言接口训练 25 May 2011 Introduction At the Googl ...
- Python全局变量和局部变量
全局变量和局部变量 定义在函数内部的变量拥有一个局部作用域,定义在函数外的拥有全局作用域. 局部变量只能在其被声明的函数内部访问,而全局变量可以在整个程序范围内访问.调用函数时,所有在函数内声明的变量 ...
- L1和L2特征的适用场景
How to decide which regularization (L1 or L2) to use? Is there collinearity among some features? L2 ...
- 读书笔记--C陷阱与缺陷(四)
第四章 1. 连接器 C语言的一个重要思想就是分别编译:若干个源程序可在不同的时候单独进行编译,恰当的时候整合到一起. 连接器一般与C编译器分离,其输入是一组目标模块(编译后的模块)和库文件,输出是一 ...
- 嵌入式 探讨父子线程、进程终止顺序不同产生的结果_skdkjxy_新浪博客
嵌入式 探讨父子线程.进程终止顺序不同产生的结果 Linux下编程,线程.进程退出顺序问题纷纷扰扰,如果父进程/线程先于子进程/线程终止,系统会做什么处理呢?反之,如果子进程/线程先于父进程/线 程终 ...
- 第一次ActiveX Fuzzing测试
接着上一篇的看雪Exploit me试题. 这道题给出了一个ActiveX的DLL,挖掘这个DLL中的漏洞. 由于从来没有接触过ActiveX的Fuzzing,所以找了一些文章来看.自己动手试验了一下 ...
- MEF实现设计上的“松耦合”(二)
介绍了下MEF的基础用法,让我们对MEF有了一个抽象的认识.当然MEF的用法可能不限于此,比如MEF的目录服务.目录筛选.重组部件等高级应用在这里就不做过多讲解,因为博主觉得这些用法只有在某些特定的环 ...
- MFC+WinPcap编写一个嗅探器之一(准备)
知识准备: MFC:http://www.jizhuomi.com/software/257.html WinPcap:http://www.ferrisxu.com/WinPcap/html/ind ...
- C语言:输入一个多位的数字,12345,求各位相加1+2+3+4+5=15
题目: 输入一个多位的数字,12345,求各位相加1+2+3+4+5=15(10分)题目内容: 输入一个多位的数字,1求各数位相加. 例如输入12345,则计算1+2+3+4+5=15 输入格式: 一 ...
- UBB/HTML互相转换简单实现源码一览
查看源码,主要用的就是正则匹配,多的不说,直接读码. 资源原地址:在线UBB/HTML转换 效果图如下 以下源码: <!DOCTYPE html> <html lang=" ...