1

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
5
  • typeof(obj === 'Object') will always return "boolean" which is truthy Commented Dec 28, 2017 at 13:09
  • @Andreas then what is the solution Commented Dec 28, 2017 at 13:10
  • 1
    Check the documentation on the typeof operator Commented Dec 28, 2017 at 13:11
  • @SudhirMN use typeof(obj) instead of typeof(obj === "Object) Commented Dec 28, 2017 at 13:13
  • (typeof(obj )=== 'Object') instead of typeof(obj === 'Object') Commented Dec 28, 2017 at 13:14

2 Answers 2

1

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;
        }
Sign up to request clarification or add additional context in comments.

Comments

0
function isSetDisable(obj) {
           return typeof(obj) === 'object' && Object.keys(obj).length === 0 ? true : false;
        }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.