2014-09-25 121 views
2

我有需要使用同样的决心两种状态:共享在UI的路由器“解析”

.state('state', { 
    url: '/somewhere', 
    templateUrl: '/views/somewhere.html', 
    controller: 'myController', 
    resolve: { 
    //resolve something here 
    } 
}) 

.state('state2', { 
    url: '/somewhere-else', 
    templateUrl: '/views/somewhere-else.html', 
    controller: 'myController', 
    resolve: { 
    //resolve the same thing here 
    } 
}) 

但是这将是很好不写相同的代码,所以有可能是各国分享一个决心?

回答

1

可能共享一个服务方法可以帮你做。

.state('state', { 
    url: '/somewhere', 
    templateUrl: '/views/somewhere.html', 
    controller: 'myController', 
    resolve: { 
    service:function(myService){ 
    return myService.someFunc(); 
    } 
    } 
}) 

.state('state2', { 
    url: '/somewhere-else', 
    templateUrl: '/views/somewhere-else.html', 
    controller: 'myController', 
    resolve: { 
    service:function(myService){ 
    return myService.someFunc(); 
    } 
    } 
}) 
+0

部分在你到底是 '共享',你将有服务代码只是一次,重复的代码只是样板,但是服务上的任何更改都会反映在任何参考中。 – 2014-09-26 12:46:45

1

您可以使用https://github.com/angular-ui/ui-router/wiki/Nested-States-%26-Nested-Views#dot-notation,如:

.state('myparent', { 
     abstract: true, 
     templateUrl: 'views/myparent.html', 
     resolve: { 
      issessionedin: function(Sessions){ 
       return Sessions.isSessionedIn(); 
      } 
     } 
    }) 
    .state('myparent.state', { 
     url: '/somewhere', 
     templateUrl: '/views/somewhere.html', 
     controller: 'SomewhereController' 
    }) 
    .state('myparent.state2', { 
     url: '/somewhere-else', 
     templateUrl: '/views/somewhere-else.html', 
     controller: 'SomewhereElseController', 

    }) 

在myparent.html你应该把

<div data-ui-view></div>