angularjs - Using ui-router and controllerAs can I simplify the adding of services? -


i have code:

   var home = {         name: 'home',         template: '<div data-ui-view></div>',         url: '/',         templateurl: 'app/access/partials/home.html',         controller: ['accessservice', function (accessservice: iaccessservice) {                 this.ac = accessservice;             }],         controlleras: 'home'     };      var homeaccess = {         name: 'home.access',         url: 'access',         templateurl: 'app/access/partials/webapi.html',         controller: ['accessservice', function (accessservice: iaccessservice) {                 this.ac = accessservice;             }],         controlleras: 'homeaccess',         resolve: {             abc: ['accessservice', function (accessservice) {                 return accessservice.getabc();             }],             def: ['accessservice', function (accessservice) {                 return accessservice.getdef();             }]         }     }; 

now using controlleras there way can simplify code eliminate adding accessservice both of controllers , 2 parts of resolve? if did how access service inside home.html , webapi.html?

there few different ways, thinking out loud here.

your home state parent of home.access , uses ui-view show child state. such template child state can reference controller in parent state. regular inheritance of $scope in angular views, although it's cleaner b/c using controlleras syntax.

for example, views might end looking this:

<home-template>   <p>{{home.somevalue}}</p>    <!-- included ui-view -->   <home-access-template>     <p>{{homeaccess.anothervalue}}     <!-- works b/c home on parent scope -->     <p>{{home.someothervalue}}</p>   </home-access-template>  <home-template> 

so if makes sense in scenario, need inject accessservice parent controller. child views use service through methods of parent controller.

a similar thing can done resolves: declaring them on parent state, available child states. this more useful when there many child states given parent.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -