Javascript 如何设置select语句的值?
当我选择一个人时,我希望favoriteThing选择器显示他们当前的选择Javascript 如何设置select语句的值?,javascript,angularjs,Javascript,Angularjs,当我选择一个人时,我希望favoriteThing选择器显示他们当前的选择 <div ng-controller='MyController'> <select ng-model='data.selectedPerson' ng-options='person.name for person in data.people'></select> <span ...> likes </span>
<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>
我是否需要设置服务并添加手表,或者是否有一种[好]方法可以直接在选择中使用最喜欢的东西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作为
添加到ng选项
将允许您基于其id而不是其引用来选择数据.things
条目。将ng model
更改为data.selectedPerson.favorite\u-id
将根据selectedPerson.favorite\u-id
自动将角度更改为正确的选项
jsfiddle:做了我想做的事-但这很不令人满意
$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>
Whoops-这不太正确。现在,我们不再选择data.thing,而是选择data.selectedPerson.favorite\u thing\u id。不再有对data.thing的引用,也不容易找到它-是吗?Angular的双向绑定就是这样工作的。如果不需要双向绑定,请尝试以下方法:。如果有帮助,请告诉我。
<select ... ng-model='data.thing' ng-options='t.name for t in data.things' association='{id: "data.selectedPerson.favorite_thing_id"}'></select>