1

I have a structure like this:

export var CustomDropdown: any[] = [
    { 
        class_name: "fonts-list", 
        list: false,
        ...
    }
]

After initializing this variable, I create another one:

CustomDropdown.push(
    { 
        class_name: "colors-list", 
        list: CustomDropdown[0].list,
        ...
    }
)

You see, the second time I instance this structure, I refer list from the first one. That's working fine.

The thing is, I want the list key to always maintain itself updated with the first one (if the first one's list becomes true, the second's one should also).

The thing is, that does not happen, and I believe it's because, when the second element is made, it just reads the list value of the first, but it does not create any connection between them.

So I believe something like a C pointer would fix my problem, or any other strategy that I do no know.

Also, I do not want a function to control this, only if its impossible. Can anyone help me?

1 Answer 1

1

Assuming list will be end up being an Array as the name implies (that you initialized with false), you could do this instead:

export var CustomDropdown: any[] = [
    { 
        class_name: "fonts-list", 
        list: [],                  // empty Array (1)
        ...
    }
]

CustomDropdown.push(
    { 
        class_name: "colors-list", 
        list: CustomDropdown[0].list, // "reference" sharing of (1)
        ...
    }
);

From here, if you do CustomDropdown[i].list.push(element), this will be reflected in CustomDropdown[0].list.

Other than that, you can't really pass the value of a primitive such as boolean by sharing ("reference") I believe.

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

1 Comment

I believe the problem with this is that my 'list' value will become true by the use of a checkbox, and Angular's automatic ngModel (I want this one checkbox to affect every instance of this object). So a CustomDropdown[i].list.push(element) never happens

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.