0

I have an Angular 1.x app and I want to initialize some $scope variables:

$scope.listing = {};
$scope.listing.title = 'This is a test';
$scope.listing.description = 'blah';

I also want to initialize an empty object, as follows:

$scope.listing.payment_types._ids = {};

This fails:

angular.js:14328 TypeError: Cannot read property '_ids' of undefined

Seems I must do this:

$scope.listing.payment_types = {};
$scope.listing.payment_types._ids = {};

It seems long-winded, is there a more concise way?

1 Answer 1

3

Nest the literals:

$scope.listing.payment_types = { _ids: {} };

The advantage of the object literal notation is, that you are able to quickly create objects with properties inside the curly braces.

For more information, see

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.