得到选择的NG-对象,NG-变化 [英] getting the ng-object selected with ng-change

查看:162
本文介绍了得到选择的NG-对象,NG-变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下选择元素

<select ng-options="size.code as size.name for size in sizes " 
        ng-model="item.size.code" 
        ng-change="update(MAGIC_THING)">
</select>

有没有办法让MAGIC_THING等于当前选定的大小,所以我有机会在我的控制器size.name和大小。code?

Is there a way to get MAGIC_THING to be equal to the currently selected size, so I have access to size.name and size.code in my controller?

大小。code会影响很多应用程序的其他部分(图片网址等),但是当item.size的NG-模式。code被更新,item.size.name需求要面临的东西用户以及更新。我认为这样做的正确方法是捕捉变化事件,并设置我的控制器内的值,但我不知道我能传递到更新,以获得正确的价值观。

size.code affects a lot of the other parts of the app (image urls, etc), but when the ng-model of item.size.code is updated, item.size.name needs to be updated as well for the user facing stuff. I assume that the correct way to do this is capturing the change event and setting the values inside of my controller, but I'm not sure what I can pass into update to get the proper values.

如果这是完全错误的方式去了解它,我很想知道正确的方式。

If this is completely the wrong way to go about it, i'd love to know the right way.

推荐答案

而不是设置NG-模型来item.size code,如何将其设置为大小:

Instead of setting the ng-model to item.size.code, how about setting it to size:

<select ng-options="size as size.name for size in sizes" 
   ng-model="item" ng-change="update()"></select>

然后在你的更新()方法 $ scope.item 将被设置为当前选择的项目。

Then in your update() method, $scope.item will be set to the currently selected item.

和需要的任何code item.size。code ,可以通过 $ scope.item。$ C得到财产$ C

And whatever code needed item.size.code, can get that property via $scope.item.code.

小提琴

更新根据意见的详细信息:

Update based on more info in comments:

使用一些其他的$ scope属性为您选择NG-模型,则:

Use some other $scope property for your select ng-model then:

<select ng-options="size as size.name for size in sizes" 
   ng-model="selectedItem" ng-change="update()"></select>

控制器:

$scope.update = function() {
   $scope.item.size.code = $scope.selectedItem.code
   // use $scope.selectedItem.code and $scope.selectedItem.name here
   // for other stuff ...
}

这篇关于得到选择的NG-对象,NG-变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆