2016-04-14 7 views
2

現在、ui-routerを知るようになっています。私はこれを持っている:ui-routerビューは変数を受け取ります

$stateProvider 
.state('taxonomy', { 
    url: '/lista/:taxonomy', 
    views: { 
     '' : { 
      templateUrl: '/js/app/taxonomy/index.tpl.html', 
      controller: 'taxonomy.index.controller', 
      controllerAs: 'vm', 
      myClass: 'classForThisOne' 
     }, 
     '[email protected]': { 
      templateUrl: '/js/app/taxonomy/sidebarRight.tpl.html', 
      /* no controller = no variable */ 
     } 
    } 
}) 

.state('taxonomy.detail', { 
    url: '/:mode/:taxonomyId?', 
    views: { 
     '[email protected]' : { 
      templateUrl: '/js/app/taxonomy/detail.tpl.html', 
      controller: 'taxonomy.detail.controller', 
      controllerAs: 'vm', 
      myClass: 'myClassForThisController' 
     }, 

     '': { 
      myClass: 'myClassForController taxonomy.index.controller' 
     } 
    } 
}); 

私は何をできるようにしたいと思い、私はcontrollercontrollerAs変数で指定されたコントローラ内部の「MyClassの」プロパティのできるようにすることです。これは何とかできますか?

私は$stateChangeStartイベントと$stateProvider.decorator('views', function (state, parent) { })を見ようとしましたが、運がありませんでした。

+0

あなたは、 –

+0

http://plnkr.co/edit/isCh8bEszuJEf6fpJ4yh?p=preview おおよその。 – chrney

答えて

1

あなたは、このようなカスタムデータを通過することができます:$stateを注入し、データを取得し、あなたのコントローラに続いて

views: { 
    '' : { 
     templateUrl: '/js/app/taxonomy/index.tpl.html', 
     controller: 'taxonomy.index.controller', 
     controllerAs: 'vm', 
     // the 'data' property can be used for custom data 
     data: { 
      myClass: 'classForThisOne' 
     } 
    }, 

function myCtrl($state){ 
    var myClass = $state.current.data.myClass; 
} 

は、参考のために参照してください:https://github.com/angular-ui/ui-router/wiki#attach-custom-data-to-state-objects

注意を上記のことがベストプラクティス( 'データ'オブジェクトを使用する)ですが、あなたも同様に行うことができます:

views: { 
    '' : { 
     templateUrl: '/js/app/taxonomy/index.tpl.html', 
     controller: 'taxonomy.index.controller', 
     controllerAs: 'vm', 
     myClass: 'classForThisOne' 
    }, 

と検索:

var myClass = $state.current.myClass; 
+0

Nah - $ state.current.dataと$ state.current.myClassは私に空の( 'undefined'のような)値を与えます。 – chrney

0

私はこれを試していませんでしたが、これは動作するはずです。あなたのコントローラーに以下の書き込みをしてください:

$scope.getClass = function() { 
    var currentState = $state.current; 
    // Or use any other named view instead of "[email protected]" 
    return currentState.views['[email protected]'].myClass; 
}; 
+0

しかし、コントローラのビューの名前が分からないので、私は「sidebarRight @」をハードコードする必要があります。 – chrney

関連する問題