简体   繁体   English

从其他来源在angularjs select指令中设置所选项目

[英]Setting selected item in angularjs select directive from a different source

I'm trying to create a form with Angular JS that lets you edit a venue (address, city etc). 我正在尝试使用Angular JS创建一个表单,该表单可让您编辑场所(地址,城市等)。

The backend runs Django and is available through a REST API (Django Rest Framework) which I'm talking to via Restangular services. 后端运行Django,可通过我正在通过Restangular服务与之交谈的REST API(Django Rest Framework)获得。 This all works fine. 这一切都很好。

For most form elements, things are pretty standard. 对于大多数表单元素来说,事情都是很标准的。 I have a venue object and just populate the items with, for example: 我有一个会场对象,只需使用以下内容填充项目:

<input type="text" ng-model="venue.street">

However, every venue object has a category which, on the backend, is a foreign key to a category object, so in Django this would be: 但是,每个场所对象都有一个类别,该类别在后端是类别对象的外键,因此在Django中,它将是:

category = models.ForeignKey(Category)

Now, when getting a venue through the REST API, the category is just referenced by the pk/id of the category object. 现在,通过REST API获取场所时,类别仅由类别对象的pk / id引用。 So, for example, this would be: 因此,例如,这将是:

{
    "id": 14,
    "name": "test place",
    "slug": "test-place",
    "country": "Ireland",
    "city": "Dublin",
    [...etc...]
    "category": 1
},

And a separate REST endpoint, gets me the categories: 还有一个单独的REST端点,为我提供了以下类别:

[
{
    "id": 1,
    "name": "Rock"
},
{
    "id": 2,
    "name": "Classic"
},
{
    "id": 3,
    "name": "Jazz"
}
]

My problem is that when editing the venue, I would like the drop down menu for categories to show categories by name but just feed the category ID to the backend, as well as pre-select the venue's current category when first shown. 我的问题是,在编辑场所时,我希望类别的下拉菜单按名称显示类别,但只将类别ID馈送到后端,并在首次显示时预选择场所的当前类别。

I'm new to Angular but, as far as I can understand, Angular populates a select directive with references to the object itself, not something simple like an ID. 我是Angular的新手,但据我了解,Angular会在select指令中填充对对象本身的引用,而不是像ID这样简单的东西。

At the moment I have this: 目前,我有这个:

<select ng-model="venue.category" ng-options="category.name for category in categories track by category.id"></select>

Obviously, this doesn't work as, even though venue.category is just a number, and category.id also, they are not the same object. 显然,这是不起作用的,即使place.category只是一个数字,而且category.id也不是相同的对象。

I'm probably missing an obvious solution here but would appreciate any pointers. 我可能在这里错过了一个显而易见的解决方案,但希望您能指点一下。

Thanks! 谢谢!

Track by is for when you want two different objects to be equal in regards to the select box. 跟踪依据是指当您希望两个不同的对象在选择框方面相等时。 This isn't the case. 事实并非如此。 You just want the model to be the id itself. 您只希望模型本身就是id。 Angular supports this like so. Angular这样支持。

http://jsfiddle.net/XLpFN/ http://jsfiddle.net/XLpFN/

Example: 例:

<select ng-model="venue.category" ng-options="category.id as category.name for category in categories"></select>

$scope.categories = [ 
    {"id": 1, "name": "Rock"}, 
    {"id": 2, "name": "Classic"}, 
    {"id": 3, "name": "Jazz"}
];

$scope.venue =  {
    "id": 14,
    "name": "test place",
    "slug": "test-place",
    "country": "Ireland",
    "city": "Dublin",
    "category": 2
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM