Unapedra
Unapedra

Reputation: 2373

AngularJS - NgOptions from function returning undefined values

I've got an application which has to show some options inside a select depending on a previous option. To achieve this, I've put the logic to show those options inside a controller function, like this:

vm.getScopeValues = function(tp){
    var vals = [];

    vals.push({n: 'Opt1', v: 'opt1'});
    
    if(!tp || tp != 'somevalue'){
        vals.push({n: 'Opt2', v: 'opt2'});
    }
    
    return vals;
};

The problem is that sometimes, I get an undefined as the value of the select options. On the following jsFiddle you can see a simpler approach, but with same result:

https://jsfiddle.net/nr9ffrkk/

As you can see:

I've made console.log to watch the returning values of the function, and it gets called correctly and returns the right values (nothing undefined).

I need to be able to create dynamically new entries on the fields array (with an initial value for type, of course) and that the select gets the right values for the options.

UPDATE: as you can see, making a controller variable (not a function) with the values does not work neither, bringing the same problem:

https://jsfiddle.net/dgy5ryvh/

Thank you!

Upvotes: 1

Views: 68

Answers (0)

Related Questions