angular - reusable dialogs

后端 未结 3 1733
小鲜肉
小鲜肉 2021-01-02 21:51

I need to build a dialog to be used with any item on a list of items. The dialog is pretty much the same regardless of the item except for the values of the fields which are

3条回答
  •  一生所求
    2021-01-02 22:31

    If you're only ever going to display one dialog like that at a time and you will use it frequently, you don't have to re-bind it to another scope, just change the data on the scope. Something like this:

    1. Create a service for your dialog
    2. Create the directive and inject your service into it. When the linking function executes, pass something like $scope.dialogData to the service so that the service can update the data.
    3. Create a controller that gets the service injected. Set the dialog data through the service to display the dialog. Since you're modifying data in your controller that's on the directives scope, Angular notices that and updates your dialog.
    4. Add ng-show on your dialogs wrapper to make it simple to implement open()/close() methods on your service.

    Now you have a dialog that can be used from anywhere in your system, and you're just re-using the same directive without having to mess with the DOM or compilation.

提交回复
热议问题