Tyler L
Tyler L

Reputation: 845

My switch/case is working but giving a console error "Cannot read property 'name' of null"

Here's the working codepen http://codepen.io/TylerL-uxai/pen/ozNgVb

When I do the third drop down and choose elephant, console.log(brand.name) says elephant. It will even do the rest of the switch statement.

But there's a console error for some reason.

$scope.changedValue3 = function (brand) {
 console.log(brand.name); // this isn't null! it returns elephant to console.
 switch (brand.name){ // cannot read property name of null
   case "elephant":
     console.log("worked"); // says "worked" when the code is run
     break;
 }
}

Upvotes: 0

Views: 203

Answers (1)

EranSch
EranSch

Reputation: 640

It looks like the issue has to do with a collision over the ng-model set on the select element. Because $scope.logos is where the data is stored, you'll need something different for ng-model. This works out on my end:

<select ng-model="logo" ng-options="item as item.brand for item in logos" ng-change="changedValue3(logo)" class="form-control">

Note that ng-model has been changed as well as the function in ng-change in order to pass the model to the function.

Here is my fork if you want to check it out in the flesh.

Upvotes: 2

Related Questions