【发布时间】:2018-03-15 10:56:35
【问题描述】:
$scope.populateMap=[{name: "ABC", code: "123"}, {name: "XYZ", code: "345"}]
//要发送模型名称+模型值当前发送ngObject.MainObj.specificFormatObj
HTML
<select ng-model="ngObject.MainObj.specificFormatObj" ng-change="ngObject.MainObj.specificFormatObj">
<option></option>
<option ng-repeat="i in populateMap" value="{{i}}">{{i.name}}</option>
JS
// CONTROLLER CODE JSON parse object to get name and code GOT parsedObj
$scope.genericSetLookups=function (Obj) { // want to do something like get the ngmodel string + the value, currently only value comes in
Obj.code=parsedObj.code;
Obj.name=parsedObj.name
};
更多解释:ngObject.MainObj.specificFormatObj
- 我希望在我的模型中以特定方式存储查找值,包括名称和代码。在我使用 ng-repeat 填充的 UI 上,因此当我选择特定值时,我可以将 i.name 作为显示并将值设置为 i.code 。
- 但如果我这样做,我的 ngObject.MainObj.specificFormatObj.name 将为 null,并且值将通过使用 ng-model 设置为 ngObject.MainObj.specificFormatObj.code,所以这就是我采用值的原因,而不是 i.code 或 i.value ,现在在地图中我有代码和名称对。
- 我将它发送到一个函数并解析它,并将值分别设置为 ngObject.MainObj.specificFormatObj.code=inputTofunc.code 作为名称。在这种情况下,在 ng-change 中我传递 ngObject.MainObj.specificFormatObj.code ,而是我想将地图中的 i 设置为 ngObject.MainObj.specificFormatObj 将其发送到函数中,模型字符串在这种情况下为“ ngObject.MainObj.specificFormatObj”。
- 所以对于 10 次查找,我可以编写一个通用代码,其中模型名称和模型值我可以作为参数发送到函数并将其设置在那里,上述方法可能是我想要设置为模型的硬编码值特定格式。
【问题讨论】:
-
你想做什么显然是无法理解的。你的问题很糟糕。您能否尝试创建一个完整的示例来说明您正在尝试什么以及您想要实现什么?
-
好的,让我再解释一下,在 ng-change 中我可以将模型的值 + 模型的名称作为字符串传递,以便我可以使用该模型名称并设置值。因此,每次我调用 genericSetLookups 函数时,模型的名称都会改变。
-
你为什么需要
ng-change在这里你的模型保存你选择的值。 -
model 当前持有 i 的值,它是具有来自`populateMap
. So when i usengchange` 的名称和代码的对象,并调用一个函数来更改格式,以便我可以在模型中以正式的形式设置值像ngObject.MainObj.specificFormatObj.code=123,ngObject.MainObj.specificFormatObj.name=ABC一样,不确定我是否可以在没有 ngchange 的情况下做到这一点。感谢您的帮助。 -
@NeverGiveUp161 使用
.indexOf找到它的索引并以这种方式选择它。这是一个演示:Plunker
标签: javascript angularjs angularjs-directive angularjs-scope angular-controller