Sirojan Gnanaretnam February 2016

Angular Attribute Always Undefined

I am trying to bind the css class using angularjs function. It's working properly but It always printing so many error on browser console. can you please any help me on this matter??. I have attached my code and screenshot below.

 <tr ng-repeat-start="inventory in inventoryDetails.HotelFareInventoryAllotmentDetails">
                                <td  rowspan="2">{{inventory.HotelRoomCategoryName}}</td>
                                <td>Inventory</td>
                                <td ng-class="getClassName(1)">{{inventory.D1.Inventory}}</td>
                                <td ng-class="getClassName(2)">{{inventory.D2.Inventory}}</td>
                                <td ng-class="getClassName(3)">{{inventory.D3.Inventory}}</td>
                 {{inventory.D31.Inventory}}</td>                              
                            </tr>
                            <tr ng-repeat-end>
                                <td>CutOff</td>
                                <td ng-class="{{getClassName(1}})">{{inventory.D1.Cutoff}}</td>
                                <td ng-class="getClassName(2)">{{inventory.D2.Cutoff}}</td>
                                <td  ng-class="getClassName(3)">{{inventory.D3.Cutoff}}</td>

                            </tr> 

  $scopeChild.getClassName = function (dayID) {

                                var dayIndex = parseInt(dayID) - 1;
                                if ($scopeChild.Days[dayIndex].IsWeekend) {
                                    return 'weekend';
                                }
                                else {
                                    return '';
                                }



                        }

Answers


Walfrat February 2016

Quick and dirty solution :

if ($scopeChild.Days && $scopeChild.Days[dayIndex] && $scopeChild.Days[dayIndex].IsWeekend){}

You may need this specially if your data are loaded asynchronously.

If you're interested about to push more the investigation i'll need to see your whole controller definition.


Lajos Arpad February 2016

From the error I can see that $scopeChild exists and has a member called Days. However, $scopeChild.Days[dayIndex] is undefined. As a result, IsWeekend is obviously not an existent property of your undefined object. As a result, you get the error. There are two cases:

  • you might want to make sure that $scopeChild.Days[dayIndex] will exist whenever you call this function, by fixing the logic of your code
  • you might want to make sure that the error will not happen, even if the function is called in the wrong way

If you want to write a library, then you need an idiot-proof solution, but if the problem lies in your algorithm, then you need to rethink it. For instance, if one looks at dayID, he/she should realize that the values of dayID should be either between 0..6 or 1..7. What is the ID of Monday in your case? Are you sure that the parameter dayID is always numeric? No trailing white characters? Is that number existent in Days? Etc., etc. I bet your problem is that the algorithm is flawed and I suggest that you should debug. Example:

  $scopeChild.getClassName = function (dayID) {

                                var dayIndex = parseInt(dayID) - 1;
                                if (!$scopeChild.Days[dayIndex]) {
                                    dayIndex = "Busted..."; //put a breakpoint here and see what happens
                                    return;
                                }
                                if ($scopeChild.Days[dayIndex].IsWeekend) {
                                    return 'weekend';
                                }
                                else {
                                    return '';
                                }



                        }

Also, as vkrishna already pointed out, you

Post Status

Asked in February 2016
Viewed 1,763 times
Voted 12
Answered 2 times

Search




Leave an answer