2014-09-25 117 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>