如何设置select语句的值
How do I set the value for a select statement?
http://jsfiddle.net/WcJbu/
当我选择一个人时,我希望最喜欢的东西选择器显示他们当前的选择。
<div ng-controller='MyController'>
<select ng-model='data.selectedPerson' ng-options='person.name for person in data.people'></select>
<span ...> likes </span>
<select ... ng-model='data.favoriteThing' ng-options='thing.name for thing in data.things'></select>
</div>
$scope.data.people = [{
name: 'Tom',
id: 1,
favorite_thing_id: 1
}, {
name: 'Jill',
id: 2,
favorite_thing_id: 3
}];
$scope.data.things = [{
name: 'Snails',
id: 1
}, {
name: 'Puppies',
id: 2
}, {
name: 'Flowers',
id: 3
}];
我需要设置一个服务并添加手表吗?或者有没有一种[好]的方法可以直接在select中使用favorite_thing_id?
将第二个选择更改为:
<select ng-show='data.selectedPerson' ng-model='data.selectedPerson.favorite_thing_id'
ng-options='thing.id as thing.name for thing in data.things'></select>
将thing.id as
添加到ng-options
将允许您根据其id而不是引用来选择data.things
条目。将ng-model
更改为data.selectedPerson.favorite_thing_id
将使角度自动更改为基于selectedPerson.favorite_thing_id
的正确选项。
jsfiddle:http://jsfiddle.net/bmleite/4Qf63/
http://jsfiddle.net/4Qf63/2/做我想做的事,但这很不令人满意。
$scope.$watch(function() {
return $scope.data.selectedPerson;
}, function(newValue) {
if (newValue) {
$scope.data.thing = $filter('filter')($scope.data.things, {id: newValue.favorite_thing_id})[0];
}
})
我希望从select语句中看到所有这些都是可能的。也许我会写一个指令。关联={key:matchValue}这样我就可以做
<select ... ng-model='data.thing' ng-options='t.name for t in data.things' association='{id: "data.selectedPerson.favorite_thing_id"}'></select>
相关文章:
- SELECT FROM WHERE语句在使用变量时不起作用…
- 使用SQLite FTS3,如何连接或嵌套SELECT语句以获得行加代码段
- 动态生成 jQuery select 语句
- .post javascript with PHP 以启用 select 语句返回
- 从 select 中获取 ID,并在 if 语句中使用它们
- 在 Ruby on Rails 中使用带有 If 语句的 Select Option 表单
- 如何对返回数据列表的清除Select语句进行编码
- 使用php的动态变量从sql select和update语句的下拉列表中获取
- 使用JavaScript填充HTML select语句
- 定义函数并在select语句中用作onchange,而不是直接使用.change(function)
- 如何在select表单中使用if-else语句
- 使用select语句进行动态图像交换
- Prototype - select语句对某些元素不起作用
- 从select html标签中选择的值在javascript if/else语句中没有产生正确的结果
- HTML5SQL使用SELECT语句从web数据库检索记录
- 如何在HTML中使用if语句(JavaScript)和select标记
- 从javascript while循环中创建一个select语句
- 如何对两个select语句进行分页
- 如何设置select语句的值
- 我怎么能调用SELECT语句和打印结果在我的网页上每5分钟