1
$scope.populateMap=[{name: "ABC", code: "123"}, {name: "XYZ", code: "345"}]

//Want to send model name + value of model Currently sending 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
};

More Explanation: ngObject.MainObj.specificFormatObj

  • I want in my model to store value of lookups in a specific way, with name and code. On the UI I populate using ng-repeat , So when I select a particular value I can either take i.name as display and set value as i.code .
  • But if i do that my ngObject.MainObj.specificFormatObj.name will be null and the value will get set to ngObject.MainObj.specificFormatObj.code by using ng-model ,so that is the reason in value I am taking i, not i.code or i.value ,now in the map i have code and name pair.
  • I sent it to a function and parse it, and set the value to ngObject.MainObj.specificFormatObj.code=inputTofunc.code respectively for name. In this case in the ng-change i pass on the ngObject.MainObj.specificFormatObj.code ,rather i want to set i from the map to ngObject.MainObj.specificFormatObj send it to function also the model string which in this case would be "ngObject.MainObj.specificFormatObj" .
  • So for 10 lookups i can write a generic code ,where the model name and model value i can send as parameter to function and set it there, the above way am doing is probably hardcoding values which i want to set to model in a specific format.
Sébastien
  • 11,860
  • 11
  • 58
  • 78
NeverGiveUp161
  • 824
  • 12
  • 33
  • 1
    It's clearly not understandable what you trying to do. You questions is horrible formated. Could you please try create a full qualified example of what you are trying and what you are trying to achieve? – lin Mar 15 '18 at 11:04
  • ok, let me try to explain a bit more, inside the ng-change can i pass on the value of model + the name of model as string, so that i can use that model name and set values to it.So the name of the model can change everytime i call the genericSetLookups function. – NeverGiveUp161 Mar 15 '18 at 11:07
  • Why do you need `ng-change` here your model holds your selected value. – lin Mar 15 '18 at 11:08
  • model is currently holding the value of i which is object having name and code from` populateMap`. So when i use `ngchange` and call a function to change the format so that I can set value inside model in a formal like `ngObject.MainObj.specificFormatObj.code=123,ngObject.MainObj.specificFormatObj.name=ABC` , not sure if i can do it without ngchange. Thanks for your help. – NeverGiveUp161 Mar 15 '18 at 11:11
  • @NeverGiveUp161 use `.indexOf` to find its index and select it that way. Here is a demo: [Plunker](https://plnkr.co/edit/sOr6A2Th9JPiGRwjhCNm?p=preview) – Aleksey Solovey Mar 15 '18 at 11:12
  • OK, this is not my cup of tea. I realy don't undestand what you are trying to achieve. Instead of focusing a solution you may going to describe what you try to achieve. E.g. by giving a simple example. – lin Mar 15 '18 at 11:13
  • if you pass a model name by string, you can access it like $scope['ngObject.MainObj.specificFormatObj'].code .Not sure if you're looking for something like this..Sorry but your question is very unclear – Flemin Adambukulam Mar 15 '18 at 11:18
  • Sorry guys, can you give it a try now. I have added more explanations best to my capability. – NeverGiveUp161 Mar 15 '18 at 11:25
  • @FleminAdambukulam : exactly want that, how to pass model name as a string, as now the value gets passed to ng-change – NeverGiveUp161 Mar 15 '18 at 11:26
  • Please come in https://chat.stackoverflow.com/rooms/166888/i-does-not-mean-i – lin Mar 15 '18 at 11:30
  • Wouldn't it be ng-change="genericSetLookups('ngObject.MainObj.specificFormatObj',ngObject.MainObj.specificFormatObj)" – Flemin Adambukulam Mar 15 '18 at 11:35
  • but nested properties would always create a problem controller code would need to be something like $scope['ngObject']['MainObj']['specificFormatObj'] – Flemin Adambukulam Mar 15 '18 at 11:38

3 Answers3

1

Since you need to pass the model name as a parameter, pass it as a string like this from html :

ng-change="genericSetLookups('ngObject.SomeObject.abc',ngObject.SomeObject.abc)"

And in the controller as the model name contains "." we cannot use the name directly as the key. We need to parse the model name. I have cooked something up after searching a bit. Hope it works.

Controller code:

    $scope.genericSetLookups(modelName, value){
        Object.setValueByString($scope, modelName, value);
    }

    Object.setValueByString = function(o, s, val) {
    s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
    s = s.replace(/^\./, '');           // strip a leading dot
    var a = s.split('.');
    for (var i = 0, n = a.length; i < n; ++i) {
        var k = a[i];
        if (k in o) {
            if(i != n-1){
                o = o[k];
            }
            else{
                o[k] = val;
            }
        } else {
            return;
        }
    }
    return o;
  }

Credit must also go to @Alnitak for the answer here

  • I am going to give this a try and accept as answer, for now am sure it helped me partially with this line , that was my first requirement `ng-change="genericSetLookups('ngObject.SomeObject.abc',ngObject.SomeObject.abc)"'` – NeverGiveUp161 Mar 17 '18 at 09:11
  • This worked like charm :) converting was not a big deal for me but to pass the model name and the value :) Thanks – NeverGiveUp161 Aug 07 '18 at 13:34
0

I didn't really understand your problem and the comments didn't make it clearer for me. What I tried to do is give you an example of how I would handle a select box and the model.

I would loop over the options with ng-options and show the selected option by putting {{selected.name}} in the template. Ofcourse if you would want to format the selected value in anyway or react to a change you can use ng-change.

Hope it helps.

Here is my JSFiddle

digijap
  • 164
  • 9
  • I do not have any problem in populating the value, the values populate properly. My problem actually starts after ng-change , inside that function I want to send the 2 values to my function, 1 which is in ng-model and second as the string of ng-model which would be ngObject.abc or ngObject.xyz , so this field would be dynamic, i need help around this. – NeverGiveUp161 Mar 15 '18 at 14:07
  • What do you mean with: `1 which is in ng-model` If the function for ng-change would be `vm.onChange` and the model would be `vm.selected`, how would you want to see the options passed to `vm.onChange`? – digijap Mar 15 '18 at 14:10
  • sorry? What do you mean with: – did not get it. – NeverGiveUp161 Mar 15 '18 at 14:11
  • currently in office, unable to access chat, i would join from home if any of you would be available. Thanks for help, and you updated the comment earlier it was not showing text. – NeverGiveUp161 Mar 15 '18 at 14:16
  • Okay i've read it again is this what you want? `ng-change="genericSetLookups(ngObject.MainObj.specificFormatObj, populateMap[ngObject.MainObj.specificFormatObj.name])"` – digijap Mar 15 '18 at 14:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/166895/discussion-between-digijap-and-nevergiveup161). – digijap Mar 15 '18 at 14:23
  • In select you can assign the value {{selected.name}} to some model using `ng-model="ngObject.MainObj.subObj.name"`, but then in model i need `"ngObject.MainObj.subObj.code"` so what I am thinking is i just set the model as ' "ngObject.MainObj.subObj"` using the ` value of option` .And on change of this i want to send 2 values to function where 1st would be `ng-model value which could be {name:abc,code:123}' and 2nd should be the name of the model like in this case 'ngObject.MainObj.subObj' so in the JS function i can set code and name myself. For All such type of fields i can dynamically do. – NeverGiveUp161 Mar 15 '18 at 14:23
  • `ng-change="genericSetLookups(ngObject.MainObj.specificFormatObj, name of this model as string which would in this case be 'ngObject.MainObj.specificFormatObj' as a string )"` – NeverGiveUp161 Mar 15 '18 at 14:28
0

I'm not sure if I understood your question. If you want to save in your model the value code + name, maybe this code can help you:

 <select ng-model="ngObject.MainObj.specificFormatObj" ng-options="(ppm.code + '-' + ppm.name) as ppm.name for ppm in populateMap">
 </select>

jsfiddle

Josito
  • 374
  • 2
  • 9
  • as per your code, can you suggest If i want to pass this model to a function with the current value of model `345-XYZ ` and model name which is essentially `ngObject.MainObj.specificFormatObj` here , so in my function i can use these values copy these to other object in model. like `ng-click=(func(valueOfModel,nameOfModel))` like this my func dynamically take different model name and values. – NeverGiveUp161 Mar 15 '18 at 19:09
  • If you want to take the full object on your genericSetLookups function try this: https://jsfiddle.net/u46m751m/5/ I hope this helps you – Josito Mar 19 '18 at 15:08