使用AngularJS,我怎么绑定&LT;选择&GT;元素到一个属性,它经由服务更新 [英] Using AngularJS, how do I bind a <select> element to an attribute that updates via a service
问题描述
我有一个显示控制器和一个管理控制器。我的内部显示控制器,我与已经选择的项目列表下拉选择。
I have a display controller and a management controller. Inside my display controller, I have a dropdown selector with the list of items that have been selected.
我可以显示区域下拉菜单更新列表,因为他们在管理控制器添加添加项目,但我无法弄清楚如何在下拉列表中选择最新的项目。
I can get the display area dropdown to update the list, adding items as they are added in the management controller, but I cannot figure out how to select the newest item in the dropdown.
<div ng-controller="MyDisplayCtrl">
<select ng-model="item" ng-options="i.name for i in items">
</select>
</div>
我已经做了的jsfiddle来说明我的情况。不过说到底,我的问题是如何在NG-模型=项绑定到由服务更新的数据源。
I have made a jsfiddle to illustrate my situation. Ultimately, though, my question is how to bind that ng-model="item" to a data source updated by a service.
http://jsfiddle.net/whtevn/mUhPW/2/
推荐答案
好了,它看起来像我已经找到了pretty满意的答案。
Well, it looks like I've found a pretty satisfactory answer to this.
我暴露了存储对象本身通过控制器
I exposed the storage object itself through the controller
function MyDisplayCtrl($scope, ItemStore) {
$scope.items = ItemStore.items;
$scope.item = ItemStore.currentItem;
// expose the itemstore service to the dom
$scope.store = ItemStore
$scope.getItem = function(){
return(ItemStore.currentItem);
}
}
和直接处理CURRENTITEM
and then address the currentItem directly
<div ng-controller="MyDisplayCtrl">
<select ng-model="store.currentItem" ng-options="i.name for i in items">
</select>
</div>
http://jsfiddle.net/whtevn/Cp2RJ/3/
这篇关于使用AngularJS,我怎么绑定&LT;选择&GT;元素到一个属性,它经由服务更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!