项目中使用了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注意事项及区别的更多相关文章

  1. Struts2 easy UI插件

    一.easy UI是类似于jQuery UI的插件库,它提供了丰富的各种常用插件:tree.datagrid... tree插件: 语法:$(selector).tree([settings]); 常 ...

  2. 【UI插件】简单的日历插件(下)—— 学习MVC思想

    前言 我们上次写了一个简单的日历插件,但是只是一个半成品,而且做完后发现一些问题,于是我们今天尝试来解决这些问题 PS:距离上次貌似很久了 上次,我们大概遇到哪些问题呢: ① 既然想做一套UI库,那么 ...

  3. 【UI插件】开发一个简单日历插件(上)

    前言 最近开始整理我们的单页应用框架了,虽然可能比不上MVVM模式的开发效率,也可能没有Backbone框架模块清晰,但是好歹也是自己开发出来 而且也用于了这么多频道的东西,如果没有总结,没有整理,没 ...

  4. Unreal Engine 4 Radiant UI 插件入门教程(二)

    本篇章前提要求:在UE4上安装了Radiant UI插件.如果没有安装,请找其它教程(或者是笔者的其它的教程,目前正在写). 本教程的目的:探讨如何从网页元素中调用蓝图中的内容: 第一步: 写一个网页 ...

  5. target-densitydpi=device-dpi会使其他ui插件布局变小

    target-densitydpi=device-dpi会使其他ui插件布局变小 东哥说:不用rem了,把meta改成这样<meta name="viewport" cont ...

  6. 基于AngularJS的Onsen UI --Onsen UI学习笔记

    AngularJS与Onsen UI的结合,Onsen UI应用程序实际上是一个AngularJS 1应用程序. <!doctype html><html lang="en ...

  7. amazeui-js插件-ui增强-日期组件如何使用(把实例做一下)

    amazeui-js插件-ui增强-日期组件如何使用(把实例做一下) 一.总结 一句话总结:需要jquery.js和amazeui.js一切才能使用 1.amazeui中的各种js效果要怎么才能使用? ...

  8. 转AngularJS路由插件

    AngularJS学习笔记--002--Angular JS路由插件ui.router源码解析 标签: angular源码angularjs 2016-05-04 13:14 916人阅读 评论(0) ...

  9. 重大发现: windows下C++ UI库 UI神器-SOUI(转载)

    转载:http://www.cnblogs.com/setoutsoft/p/4996870.html 在Windows平台上开发客户端产品是一个非常痛苦的过程,特别是还要用C++的时候.尽管很多语言 ...

  10. 转: windows下C++ UI库 UI神器-SOUI

    转:http://www.cnblogs.com/setoutsoft/p/4996870.html 前言 在Windows平台上开发客户端产品是一个非常痛苦的过程,特别是还要用C++的时候.尽管很多 ...

随机推荐

  1. linux快速安装mysql教程

    #安装mysql服务器:yum install mysql-server #设置开机启动chkconfig mysqld on#现在启动服务service mysqld start #设置root初始 ...

  2. python基础-类的反射

    1)反射是通过字符串方式映射内存中的对象. python中的反射功能是由以下四个内置函数提供:hasattr.getattr.setattr.delattr, 改四个函数分别用于对对象内部执行:检查是 ...

  3. Shell编程学习2--命令大全

    Linux中有很多的命令,这些命令可分分为10类(具体参见[1]): 1) 文件管理; 2) 文档编辑; 3) 文件传输; 4) 磁盘管理; 5) 磁盘维护; 6) 网络通讯; 7) 系统管理; 8) ...

  4. collection.toArray(new String[0])中new String[0]的作用

    new string[0]的作用 比如:String[] result = set.toArray(new String[0]); Collection的公有方法中,toArray()是比较重要的一个 ...

  5. linux定时任务-cron

    /sbin/service crond start //启动服务 /sbin/service crond stop //关闭服务 /sbin/service crond restart //重启服务 ...

  6. No.17 selenium学习之路之判断与等待

    一.三种等待方式 1.sleep 加载time库.time.sleep() 休眠单位以秒为单位 2.implicitly_wait() 等待页面完全加载完成(左上角转圈结束) 参数为等待时间,等待页面 ...

  7. celery在Django中的集成使用

    继上回安装和使用Redis之后,看看如何在Django中使用Celery.Celery是Python开发分布式任务列队的处理库.可以异步分布式地异步处理任务,也可定时执行任务等等.通常我们可以在Dja ...

  8. java遍历ftp文件夹下所有文件(或指定文件下的文件)

    import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import org.apach ...

  9. CCF CSP 201409-3 字符串匹配

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201409-3 字符串匹配 问题描述 给出一个字符串和多行文字,在这些文字中找到字符串出现的那 ...

  10. day7 socket网络编程基础

    Socket Socket是什么? 下面来看一下网络的传输过程: 上面图片显示了网络传输的基本过程,传输是通过底层实现的,有很多底层,我们写传输过程的时候,要知道所有的过程那就太复杂了,socket为 ...