Home Ask Login Register

Developers Planet

Your answer is one click away!

Samantha February 2016

AngularJS - choosing between lists in view

I'm working in angularjs and basically what I'm trying to do is prepopulate a dropdown list depending on the organization name. Now, the following code DOES prepopulate the correct values but whenever I go to execute the code, it cannot reference the properties defined in the list.

I have two lists defined in my controller:

listOne = [{name : 'NameOne', id : 'IdOne'}];
listTwo = [{name : 'NameTwo', id : 'NameTwo'}]; 

(there's more items in these lists... but just so you get the point)

In my view I have the following:

<div ng-if="$root.sessionObj.name.org != 'OrgName'">
    <select name="domain" id = "domain" class="form-control"
         ng-options="domain as domain.name for domain in listOne"
         <option value=""/>
<div ng-if="$root.sessionObj.name.org=='OrgName'">
     <select name="domain id="domain" class="form-control"
         ng-options="domain as domain.name for domain in listTwo"
         <option value="" />

Whenever I go to execute my code, I keep receiving a type error that states "unable to get property 'name' of undefined or null reference. Does anyone know what I could be doing wrong? I tried a bunch of different things, including wrapping my divs in the controller that defines these lists, but that also did not work. Any help is appreciated!


Scott Schwalbe February 2016

That error could either be from $root.sessionObj or domain from your html. Do you have both of those defined on your scope?

Samantha February 2016

Figured it out! Turns out it was caused by something completely not related to the list.

Kito February 2016

Most likely, the undefined 'name' is coming from '$root.sessionObj.name' and not from 'domain.name'.

Make sure that '$root.sessionObj' is not undefined.

Post Status

Asked in February 2016
Viewed 1,603 times
Voted 14
Answered 3 times


Leave an answer

Quote of the day: live life