在选择选项NG-变化调用多种独特的功能 [英] ng-change on select option calling multiple unique functions

查看:125
本文介绍了在选择选项NG-变化调用多种独特的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,每个选择选项我需要调用一个不同的功能,功能不一样。

My problem is that for each select option I need to call a different function, not the same function.

我开始与吴单击但很快就意识到这是行不通的。然后我读角的文档上NG选项<一个href=\"https://docs.angularjs.org/api/ng/directive/ngOptions\">https://docs.angularjs.org/api/ng/directive/ngOptions

I started with ng-click on each select option but quickly realized this doesn't work. I then read Angular's documentation on ng-options https://docs.angularjs.org/api/ng/directive/ngOptions

文档示出了使用纳克变化但它是基于这样的事实,每个选择选项可获得适用于它的相同的功能。

The documentation illustrates using ng-change but it is based on the fact that each select option gets the same function applied to it.

在思考这个了一下我结束了创建一个被调用的NG-改变功能后。此功能只是决定调用哪个委托。

After thinking about this for a bit I ended up creating a function that gets called on ng-change. This function just determines which delegate to call.

$scope.determineAction = function() {
    var getDelegation = $injector.get($scope.selected.action);
    getDelegation.delegate();
  }

Plunker:
http://plnkr.co/edit/2Str6OqmFDH3kKdiW6i5

我创建了一个解决我的问题,但我想知道,如果这是正确的做法?我失去了在NG选项的东西,允许许多不同的函数调用?

I've created a solution to my problem but I want to know if this is the right approach? Am I missing something in ng-options that allows for many different function calls?

推荐答案

TL; DR:是,你正在做正确的事情。

TL;DR: Yes, you are doing the right thing.

是的,你正在做正确的事情。您不会错过的 NG-选项的一些秘密方面,允许你指定的东西对每个项目不同。事实上,这样做会破坏​​的 NG-选项的目的。它是专门意味着,你正在处理的项目数是可变的情况,但是需要对它们执行的操作是一样的。

Yes, you are doing the right thing. You're not missing some secret aspect of ng-options that allows you to specify something different for each item. In fact, doing this would defeat the purpose of ng-options. It's meant specifically for situations where the number of items you're dealing with are variable, but the actions that need to be executed on them are the same.

您Plunker ,你正好证明了这种。你那是可变的行动清单;它可以有任何数量的动作做。但是,你想与他们做的事情是一样的:当你选择一个新的项目,要作为你的后盾范围指定运行一些动作。这正是控制器意思做!当你发现,你需要做的是写你的函数以这样一种方式,它决定事物的当前状态,并相应地作出反应。而你的观点需要做的都是确保它正在建立的状态,这样你的控制器能够理解它。

In your Plunker, you're demonstrating this exactly. You have a list of actions that is variable; it could have any number of actions to do. However, what you want to do with them is the same: when you select a new item, you want to run some action as specified by your backing scope. This is exactly what the controller is meant to do! As you found, all you need to do is write your function in such a way that it determines the current state of things and reacts accordingly. And all your view needs to do is make sure that it is setting up that state so that your controller can make sense of it.

您正在做的这一切已经让继续努力吧!

You're doing all of this already so keep it up!

这篇关于在选择选项NG-变化调用多种独特的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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