javascript - Modal window with Angular (UI-Router / Directive) -
i have spent time looking generic way of controlling modal window angularjs
, none of proposed options anywhere near 'good' solution.
the directive solution
i found this demo, downside of have manually manage , store state of modal , cross-scope update it:
scope.$parent[attrs.visible] = true;
also if had add more functionality adding item popup involve more ugly code on parent page scope.
the ui-router solution
this official guide on how use modals ui router.
this using ui.bootstrap.modal
my question is, there simple , elegant solution quite frankly simple problem...
something example:
.state('popup', { url: '/item/add/', views: { 'popupview': { templateurl: "/mypopup.html", controller: "mypopupcontroller" } }, type: 'modal' })
and things close, redirect, submit, handled in mypopupcontroller
.
i not seeking explanation of why examples above are, or how should using them. looking if has come better solution.
i've been working time on modal/dialog plugin ui-router (utilising , ui-router-extras).
i'm in process of porting in house code open source project, , it'd huge benefit (certainly me) if open giving try.
we using in house version in our production application , working quite well. goal make open source version better in terms of consumer api , performance.
the configuration phase
var app = angular.module('mod', ['angular.ui.router.modal', /** ui-router + ui-router-extras **/]); app.config(function ($uiroutermodalprovider) { $uiroutermodalprovider.config({ viewname: 'my_modal_view_name', templateurl: '/path/to/my_modal_wrapper.html', rootstate: 'name_of_my_apps_root_state', controller: 'my_modal_wrapper_controller', resolve: { common: function ($http) { return $http.get(apiurl + '/common_modal_settings'); } } }); });
let's go through different parts:
viewname
this name of ui-view
modal template live in. required have container in page ui-view
attribute pointing view of $uiroutermodalprovider.viewname
lives beside regular ui-view
regular content (example shown later).
the package provides directive you
templateurl
this base of of modals. wanted container properties, , $scope
independent of inner contents of modal @ given point in time.
rootstate
this name of root state in application. important matches of state holds root ui-view
element , modal ui-view
element.
controller
the name of common modal controller. can function (though recommend named controller can unit tested in isolation).
resolve
the common resolve block each modal. interdependent of each modal states own resolve block. the api , behaviour regarding resolves still in flux.
the registration-of-states phase
now base behaviour configured, how register modal state:
.modalstate('home.mysupermodal', { url: '/my-super-modal', templateurl: 'supermodal.html', controller: 'supermodalcontroller' });
the .modalstate
function register state , reference common settings set in modalprovider , convert given state definition final state object, looking so:
.state('home.mysupermodal', { url: '/my-super-modal', views: { 'modal_view_name@root_state': { templateurl: 'modal_wrapper.html', controller: 'modalwrappercontroller' } }, $$originalstate: { templateurl: 'supermodal.html', controller: 'supermodalcontroller' } });
the markup
<!-- outermost ui-view element --> <html> <body ng-app="app"> <div ui-view></div> </body> </html> <!-- content root state of application {living inside outermost ui-view directive} --> <div> <ui-view></ui-view> <ui-modal-view></ui-modal-view> </div>
now container our modal wrapper set up. but inner contents?
this directive comes play; uimodalfill
. needs present in wrapping modal template. such:
<!-- wrapping modal template --> <div id="modal_wrapper"> <h1>some heading. i'm present!</h1> <hr> <ui-modal-fill></ui-modal-fill> </div>
conclusion
some final notes before (if) feel giving go:
- a lot of desired functionality has not yet been ported/implemented. it's work in progress stands.
- some of api's may change in near future, including not limited
resolve
property , way $state.resolve lives common modal.resolve. - some of boilerplate requirements due change. api should made easy use (yet flexible), right there's details consumer must fill in.
- the documentation virtually non existent. best way figure out actually going on behind scenes source (sorry...).
so, if haven't managed scare off giving go - head on over github , grab angular-ui-router-modal , give go. i'm sort of available on github and/or on if need help.
or if pester me writing documentation. understand that, fully.
Comments
Post a Comment