bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

`$modal.open` compatibility with component (`angular.component` method)

Open zdychacek opened this issue 10 years ago • 62 comments

Hello,

I want to ask if you're considering support of angular.component method which is new API for creating components.

I'd like to have the ability to call $modal.open method with the component definition. Now I have to specify controller and template options...

My API proposal:

angular.module('myApp').component('myModal', {
  bindings: {
    greeting: '<'
  },
  template: '<div>{{$ctrl.greeting}}</div>',
  controller: function () {},
})

// open the modal
$modal.open({
  component: 'myModal',
  bindings: {
    greeting: 'hello',
  },
});

Thanks for response.

O.

zdychacek avatar Mar 23 '16 12:03 zdychacek

I believe this would force us to only support Angular 1.5+ and i'm not sure we're ready to go there yet.

icfantv avatar Mar 23 '16 15:03 icfantv

@icfantv Is there any issue with coexistence of both variants? Could't proposed API coexist with current one?

zdychacek avatar Mar 23 '16 16:03 zdychacek

@zdychacek, i don't know. this would require investigation if this is, in fact, something we want to support. but we need to have the discussion first.

icfantv avatar Mar 23 '16 16:03 icfantv

I'm not against this, but we'll have to determine at what stage we want to implement this. We could implement it now, and have code where this only applies to angular 1.5+, but that adds bloat for those using 1.4.

wesleycho avatar Mar 23 '16 17:03 wesleycho

I'm trying to use existing modal with component this way:

modalInstance = this.$uibModal.open({
      template: '<my-component message="{{$ctrl.message}}"></my-component>',
      size: 'sm',
      controllerAs: '$ctrl',
      controller: () => {
        this.message = 'some message';
    });

Then in my component:

let myComponent = {
  bindings: {
    message: '@'
  }
};

However, it doesn't work :( this.message in component controller is empty. I'm not sure why.

----- edit

I've just found the problem -> arrow function in controller! :)

To make it work:

modalInstance = this.$uibModal.open({
      template: '<my-component message="{{$ctrl.message}}"></my-component>',
      size: 'sm',
      controllerAs: '$ctrl',
      controller: function() {
        this.message = 'some message';
    });

Olgagr avatar Apr 04 '16 05:04 Olgagr

@Olgagr there should be nothing wrong with having an arrow function if your transpiler is working correctly. Arrow functions are simply rewritten as you have defined above. I would encourage you to look at your transpiled code with the arrow function and see what it's generating.

icfantv avatar Apr 04 '16 15:04 icfantv

@icfantv I think in this case it makes difference because $uibModal.open is run in controller class of other component. To show wider context:

class PublicationController {

    openModal() {
        modalInstance = this.$uibModal.open({
         template: '<my-component message="{{$ctrl.message}}"></my-component>',
         size: 'sm',
         controllerAs: '$ctrl',
         controller: function() {
            this.message = 'some message';
         });
    }
}

Can you see the problem here ? If I use lambda, this will be PublicationController instance.

Olgagr avatar Apr 04 '16 15:04 Olgagr

@Olgagr, i don't follow, i use arrow functions inside my ES6 classes all the time. the only thing I don't do is specify a function for my controller property - but the transpiler should be converting them all anyway.

icfantv avatar Apr 04 '16 18:04 icfantv

@icfantv And this is a difference :) Arrow functions are not compiled to the same code as "normal" functions. See the difference: http://bit.ly/1S41ZuP

Olgagr avatar Apr 04 '16 18:04 Olgagr

ah, ok. good to know. thanks.

icfantv avatar Apr 04 '16 18:04 icfantv

@Olgagr You can't use arrow function in this case because arrow functions have lexical scope which means that this points to something else (global object or undefined in strict mode), so this is not a controller instance. In this case normal function should be used.

dfsq avatar Apr 08 '16 13:04 dfsq

I'm not sure this proposal has much sense or even necessary. The example @zdychacek gives can already be expressed with existing syntax:

// Proposal
$modal.open({
  component: 'myModal',
  bindings: {
    greeting: 'hello',
  },
});

// Can currently be implemented
$modal.open({
  template: '<my-modal greeting="$ctrl.greeting"></my-modal>',
  controller: function() {
    this.greeting = 'hello'
  },
  controllerAs: '$ctrl'
})

Existing syntax is very flexible and can already be used with components. At the same time, proposal means introducing two more config properties (component and bindings), which are redundant (again, because you can achieve the goal with existing ones). It is also a little confusing: it's not obvious that bindings will get merged (?) with component bindings and how this merge is going to happen. What happens if one provides both component and controller and template?

So there are questions that are not very obvious.

dfsq avatar Apr 08 '16 13:04 dfsq

@dfsq Yes, I know about it (arrow functions). We've just discussed this above :)

Olgagr avatar Apr 08 '16 14:04 Olgagr

I agree with @dfsq . I also use the existing syntax with components and so far it works pretty well.

Olgagr avatar Apr 08 '16 14:04 Olgagr

@dfsq Hi, I would like to know, how do you access $modaInstance service from within the component's controller?

I can't see any other solution than passing it explicitly via component's bindings from the controller.

$modal.open({
  template: '<my-modal greeting="$ctrl.greeting" modal-instance="$ctrl.modalInstance"></my-modal>',
  controller: [ '$modalInstance', function ($modalInstance) {
    this.greeting = 'hello';
    this.modalInstance = $modalInstance;
  } ],
  controllerAs: '$ctrl',
});

zdychacek avatar Apr 11 '16 15:04 zdychacek

@zdychacek Do you need the whole modal instance ? For example, to have access to close or dismiss methods you can also pass just them as bindings:

$modal.open({
  template: '<my-modal greeting="$ctrl.greeting" close="$ctrl.close()" dismiss="$ctrl.close()"></my-modal>',
  controller: [ '$modalInstance', function ($modalInstance) {
    this.greeting = 'hello';
    this.close = $modalInstance.close;
    this.dismiss = $modalInstance.dismiss;
  } ],
  controllerAs: '$ctrl',
});

and then in your component bindings:

{
   close: '&',
   dismiss: '&'
}

Olgagr avatar Apr 11 '16 16:04 Olgagr

@Olgagr No I don't need it whole. I've only tried to model the behaviour as similar as possible to the current one and that's to have an option to inject $modalInstance service into the component's controller directly.

But I can live with the solution mentionted above :)

zdychacek avatar Apr 11 '16 20:04 zdychacek

On thing that is surprising to me: bindings are not updated. Let's say I open modal like this:


class SomeComponentController {

    constructor() 
       this.pages = this.getPages(); 
    }

    open() {
         var _this = this;
         $modal.open({
             template: '<my-modal pages="$ctrl.pages""></my-modal>',
             controller: function () {
                   this.pages = _this.pages;
           }],
           controllerAs: '$ctrl',
        });
    }


}

my-modal component has bindings:

bindings: {
   pages: '<'
}

Now, at some point, when modal is already opened, inside SomeComponentController this.pages changes. However, inside my-modal component this pages binding is not refreshed ($onChanges method is not called). This is surprising. It should be live binding after all, right ?

Olgagr avatar Apr 19 '16 10:04 Olgagr

@Olgagr This should not happen, most likely there is something with the way you are updating SomeComponentController's pages.

Here is a quick demo: http://plnkr.co/edit/XeqlnwjePvSTn0pXPaPz?p=preview

dfsq avatar Apr 19 '16 12:04 dfsq

@dfsq Thanks for demo! Yes, you're right. The bindings should be updated.

Olgagr avatar Apr 19 '16 12:04 Olgagr