Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Component bindings are bound too late #270

Open
nsgundy opened this issue Aug 9, 2019 · 3 comments
Open

Component bindings are bound too late #270

nsgundy opened this issue Aug 9, 2019 · 3 comments

Comments

@nsgundy
Copy link

nsgundy commented Aug 9, 2019

When I pass a component with bindings to the ModalService.showModal() function, I would expect to have the passed values available in the $onInit() lifecycle-hook of that component (that's when all the bindings are supposed to have been initialized). However, that is currently not the case and one can observe the value only being set later. A workaround is to use the $onChanges() lifecylce-hook of the component, but that's not always convenient.

// test.component.js
export const TestComponent = {
  template: `<tabs initial-tab="$ctrl.test">...</tabs>`
  controller: function() {
    $onInit() {
      console.log(`test=${this.test}`)
    },
  }
  bindings: {
    test: '<',
    close: '<'
  }
}

// somewhere in the application
ModalService.showModal({
  component: 'testComponent',
  bindings: {
    test: 'this is a test'
  }
})

Other observations:

  • close needs to be bound as a one-way binding (>) and not an event binding (&) as I would have expected
  • Using text bindings (@) does not work. Modifying the above example, test will evaluate to the string bindings.test
  • I have not tried binding to event bindings (&) on the component, glancing at the source code I'd imagine that they won't, especially when trying to pass events with data

@DougKeller Any ideas on how to address these issues?

@nsgundy
Copy link
Author

nsgundy commented Aug 16, 2019

Right, the issue for the bindings being bound too late is that they are only put on the scope once the controller is being run. If the bindings are placed on the injected scope directly, then the issue vanishes and $onInit() inside the component has access to the bindings.

@dwmkerr
Copy link
Owner

dwmkerr commented Aug 26, 2019

Anyone able to make a quick PR w/ tests to show the correct behaviour?

DougKeller added a commit to topechelon/angular-modal-service that referenced this issue Sep 15, 2020
This ensures that component bindings are available in the $onInit callback of the component's lifecycle
This addresses the main issue brought up by dwmkerr#270
DougKeller added a commit to topechelon/angular-modal-service that referenced this issue Sep 15, 2020
This ensures that component bindings are available in the $onInit callback of the component's lifecycle
This addresses the main issue brought up by dwmkerr#270
@DougKeller
Copy link
Contributor

DougKeller commented Sep 15, 2020

Apologies, I didn't notice this issue when it was first brought up. Funny enough we ran into this ourselves internally, so I just pushed #280 to address the $onInit issue.

The attribute and output bindings issues you brought up are trickier issues, so I didn't address those here. It's not ideal, but like you noticed using a one-way input binding for these instead works.

I've added a note to the readme to reflect this.

DougKeller added a commit to topechelon/angular-modal-service that referenced this issue Sep 15, 2020
This ensures that component bindings are available in the $onInit callback of the component's lifecycle
This addresses the main issue brought up by dwmkerr#270
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants