javascript - AngularJS directive design patterns for a sidebar menu -
i working angularjs create simple side menu. have following 2 options directive design cannot decide 1 better:
option 1
the html markup:
<sidebar title="sidebar heading"> <sidebar-element name="heading">description</sidebar-element> ... </sidebar> option 2
the html markup:
<sidebar>sidebar heading</sidebar> and, data coming directly controller:
$scope.sidebarelements=[{name:'head 1', description:'description 1', isactive:true}]; consider data coming me server in json.
if go option 2, can pass data directly controller.
if go option 1, have <sidebar-element ng-repeat='element in elements' ...> in it, , pass on data it. option 1 seems 'better designed' somehow don't know if should build layer of abstraction way when not required.
which 1 of 2 better , why?
i have started work angularjs , trying find right way 'think in angularjs'
it depends on how extensible need make design. if want make design work option1 in future, should go it, because going consume more time put piece of code.
if want go highly extensible approach should create directive , directive create collection using controller data. this (option1) allow user use combination of both directives menu static or dynamic data. if decide go option1, link useful [link] : http://sporto.github.io/blog/2013/06/24/nested-recursive-directives-in-angular/ [it has live example] : http://jsbin.com/acibiv/3/edit
if data coming json (controller data) , there never need add html (static ) go option2.
i think starting, should go option2 (less reusable easier compared).
about directive naming think sidebar may contain other elements (other menu). naming should tell menu.
Comments
Post a Comment