Reputation: 125
Hi i have to disable button if i get scenariosViewAll.collectionBookObject ={} array ,For this i am using function to return below is my code:
<button" ng-disabled="scenariosViewAll.isSetDisable(scenariosViewAll.collectionBookObject)">Create Book from Collection</button>
and in controller my code is:
function isSetDisable(obj) {
return typeof(obj === 'Object') && Object.keys(obj).length === 0 ? true : false;
}
getting console error as below and it is working properly, any wrong in my code.
lib.min.js:3762 TypeError: Cannot convert undefined or null to object
at Function.keys (<anonymous>)
at scenariosViewAllController.isSetDisable (app.min.js:29729)
at fn (eval at compile (lib.min.js:3876), <anonymous>:4:415)
at m.$digest (lib.min.js:3787)
at m.$apply (lib.min.js:3790)
at lib.min.js:3803
at e (lib.min.js:3690)
at lib.min.js:3693
Upvotes: 1
Views: 46
Reputation: 13146
You should change Object
condition and add extra condition to handle possible null
values;
function isSetDisable(obj) {
return obj != null && typeof(obj) == typeof({}) && Object.keys(obj).length === 0 ? true : false;
}
Upvotes: 1
Reputation: 1600
function isSetDisable(obj) {
return typeof(obj) === 'object' && Object.keys(obj).length === 0 ? true : false;
}
Upvotes: 0